跳到内容

npm 包管理器简介

【An introduction to the npm package manager】

npm 介绍

【Introduction to npm】

npm 是 Node.js 的标准包管理器。

据报道,2022 年 9 月,npm 注册表中列出了超过 210 万个软件包,使其成为地球上最大的单一语言代码存储库,你可以肯定,几乎所有内容都有一个软件包!

【In September 2022 over 2.1 million packages were reported being listed in the npm registry, making it the biggest single language code repository on Earth, and you can be sure there is a package for (almost!) everything.】

它最初是一种下载和管理 Node.js 包依赖的方法,但后来也成为前端 JavaScript 中使用的工具。

【It started as a way to download and manage dependencies of Node.js packages, but it has since become a tool used also in frontend JavaScript.】

Yarnpnpm 是 npm 命令行工具的替代方案。你也可以去了解一下它们。

软件包

【Packages】

npm 安装、更新并管理你项目依赖的下载。依赖是预先构建的代码片段,例如库和包,你的 Node.js 应用需要它们才能正常运行。

安装所有依赖

【Installing all dependencies】

如果一个项目有 package.json 文件,可以通过运行

【If a project has a package.json file, by running】

npm install

它会在 node_modules 文件夹中安装项目所需的所有内容,如果该文件夹不存在,会自动创建它。

【it will install everything the project needs, in the node_modules folder, creating it if it's not existing already.】

安装单个软件包

【Installing a single package】

你也可以通过运行来安装特定的包

【You can also install a specific package by running】

npm install <package-name>

此外,自 npm 5 起,该命令会将 <package-name> 添加到 package.json 文件的 dependencies 中。在 5 版本之前,你需要添加 --save 标志。

【Furthermore, since npm 5, this command adds <package-name> to the package.json file dependencies. Before version 5, you needed to add the flag --save.】

通常你会看到更多标志添加到此命令:

【Often you'll see more flags added to this command:】

  • --save-dev 安装并将条目添加到 package.json 文件的 devDependencies
  • --no-save 安装但不会将条目添加到 package.json 文件的 dependencies
  • --save-optional 安装并将条目添加到 package.json 文件的 optionalDependencies
  • --no-optional 将阻止安装可选依赖

也可以使用标志的简写:

【Shorthands of the flags can also be used:】

  • -S: --save
  • -D: --save-dev
  • -O: --save-optional

devDependenciesdependencies 的区别在于,前者包含开发工具,比如测试库,而后者会在生产环境中随应用一起打包。

【The difference between devDependencies and dependencies is that the former contains development tools, like a testing library, while the latter is bundled with the app in production.】

至于 optionalDependencies,区别在于依赖构建失败不会导致安装失败。但处理缺少依赖是你的程序的责任。阅读更多关于 可选依赖 的内容。

【As for the optionalDependencies the difference is that build failure of the dependency will not cause installation to fail. But it is your program's responsibility to handle the lack of the dependency. Read more about optional dependencies.】

更新软件包

【Updating packages】

通过运行,更新也变得简单

【Updating is also made easy, by running】

npm update

npm 会检查所有包是否有符合你版本约束的更新版本。

你也可以指定要更新的单个包:

【You can specify a single package to update as well:】

npm update <package-name>

版本控制

【Versioning】

除了普通下载之外,npm 还管理 版本控制,因此你可以指定某个软件包的特定版本,或者要求使用高于或低于你需要的版本。

【In addition to plain downloads, npm also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.】

很多时候你会发现一个库只与另一个库的主要版本兼容。

【Many times you'll find that a library is only compatible with a major release of another library.】

或者最新版本的库中仍未修复的错误导致了问题。

【Or a bug in the latest release of a lib, still unfixed, is causing an issue.】

指定库的明确版本还有助于确保每个人使用完全相同的包版本,这样整个团队就在 package.json 文件更新之前保持相同的版本。

【Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json file is updated.】

在所有这些情况下,版本控制都非常有帮助,而 npm 遵循语义化版本控制(semver)标准。

【In all those cases, versioning helps a lot, and npm follows the semantic versioning (semver) standard.】

你可以通过运行来安装软件包的特定版本

【You can install a specific version of a package, by running】

npm install <package-name>@<version>

正在运行的任务

【Running Tasks】

package.json 文件支持一种格式,用于指定可以使用以下方式运行的命令行任务

【The package.json file supports a format for specifying command line tasks that can be run by using】

npm run <task-name>

例如:

【For example:】

{
  "scripts": {
    "start-dev": "node lib/server-development",
    "start": "node lib/server-production"
  }
}

使用此功能运行 Webpack 非常常见:

【It's very common to use this feature to run Webpack:】

{
  "scripts": {
    "watch": "webpack --watch --progress --colors --config webpack.conf.js",
    "dev": "webpack --progress --colors --config webpack.conf.js",
    "prod": "NODE_ENV=production webpack -p --config webpack.conf.js"
  }
}

因此,你无需输入那些容易忘记或输入错误的长命令,你可以运行

【So instead of typing those long commands, which are easy to forget or mistype, you can run】

$ npm run watch
$ npm run dev
$ npm run prod