选择命令行版本:
🌐 Synopsis
npm install [<package-spec> ...]aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
🌐 Description
此命令会安装一个软件包以及它所依赖的任何软件包。如果该软件包有 package-lock、npm shrinkwrap 文件或 yarn lock 文件,依赖的安装将由这些文件决定,并遵循以下优先顺序:
🌐 This command installs a package and any packages that it depends on. If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that, respecting the following order of precedence:
npm-shrinkwrap.jsonpackage-lock.jsonyarn.lock请参阅 package-lock.json 和 npm shrinkwrap。
🌐 See package-lock.json and npm shrinkwrap.
package 是:
🌐 A package is:
package.json 文件描述的程序的文件夹<name>@<version>(参见 registry)以及 (c)<name>@<tag>(参见 npm dist-tag),指向 (d)<name><git remote url>即使你从不发布你的包,如果你只是想写一个 node 程序(a),你仍然可以获得使用 npm 的很多好处,也许你还想在打包后能够轻松地安装它成一个 tarball (b)。
🌐 Even if you never publish your package, you can still get a lot of benefits of using npm if you just want to write a node program (a), and perhaps if you also want to be able to easily install it elsewhere after packing it up into a tarball (b).
npm install(在包目录中,无参数):
将依赖安装到本地 node_modules 文件夹中。
在全局模式下(即在命令后附加 -g 或 --global 时),它会将当前包上下文(即当前工作目录)安装为全局包。
默认情况下,npm install 会安装 package.json 中列为依赖的所有模块。
使用 --production 标志(或当 NODE_ENV 环境变量设置为 production 时),npm 将不会安装列在 devDependencies 中的模块。要在 NODE_ENV 环境变量设置为 production 时安装 dependencies 和 devDependencies 中列出的所有模块,可以使用 --production=false。
注意:在向项目添加依赖时,
--production标志没有特殊含义。
npm install <folder>:
如果 <folder> 位于你项目的根目录中,它的依赖将会被安装,并可能像其他类型的依赖一样被提升到顶层的 node_modules。如果 <folder> 位于你项目的根目录之外,npm 不会在目录 <folder> 中安装该包的依赖,但它会创建一个指向 <folder> 的符号链接。
注意:如果你想像从注册表安装包一样安装目录中的内容,而不是创建链接,则需要使用
--install-links选项。
示例:
npm install ../../other-package --install-linksnpm install ./sub-package
npm install <tarball file>:
安装一个已存在于文件系统上的软件包。注意:如果你只是想将一个开发目录链接到你的 npm 根目录,可以使用 npm link 更简单地完成此操作。
压缩包要求:
.tar、.tar.gz 或 .tgz 作为扩展名。package/)。npm 在安装软件包时会去掉一层目录(相当于执行了 tar x --strip-components=1)。name 和 version 属性的 package.json 文件。示例:
npm install ./package.tgz
npm install <tarball url>:
获取 tar 包的 URL,然后安装它。为了区分此选项和其他选项,参数必须以 "http://" 或 "https://" 开头。
示例:
npm install https://github.com/indexzero/forever/tarball/v0.5.6
npm install [<@scope>/]<name>:
执行 <name>@<tag> 安装,其中 <tag> 是“标签”配置。(参见 config。该配置的默认值为 latest。)
在大多数情况下,这将安装在 npm 注册表中标记为 latest 的模块版本。
示例:
npm install sax
npm install 默认将任何指定的包保存到 dependencies。此外,你可以使用一些额外的标志来控制它们的保存位置和方式:
-P, --save-prod:包将出现在你的 dependencies 中。这是默认设置,除非存在 -D 或 -O。-D, --save-dev:封装将出现在你的 devDependencies 中。-O, --save-optional:封装将出现在你的 optionalDependencies 中。--no-save:防止保存到 dependencies。当使用上述任何选项将依赖保存到 package.json 时,还有两个额外的可选标志:
-E, --save-exact:保存的依赖将使用精确的版本进行配置,而不是使用 npm 的默认 semver 范围运算符。-B, --save-bundle:已保存的依赖也将被添加到你的 bundleDependencies 列表中。此外,如果你有 npm-shrinkwrap.json 或 package-lock.json,它们也会被更新。
<scope> 是可选的。该包将从与指定作用域相关联的注册表中下载。如果给定作用域未关联任何注册表,则假定使用默认注册表。请参阅 scope。
注意:如果你在作用域名称中不包含 @ 符号,npm 会将其解释为 GitHub 仓库,见下文。作用域名称后也必须跟一个斜杠。
示例:
npm install saxnpm install githubname/reponamenpm install @myorg/privatepackagenpm install node-tap --save-devnpm install dtrace-provider --save-optionalnpm install readable-stream --save-exactnpm install ansi-regex --save-bundle
注意:如果当前工作目录中存在名为 <name> 的文件或文件夹,它将尝试安装该文件或文件夹,只有在其无效时才会尝试按名称获取包。
npm install <alias>@npm:<name>:
在自定义别名下安装软件包。允许同名的软件包并行存在多个版本,为原本名称较长的软件包提供更方便的导入名称,并可以使用 git 分支替代或 fork 的 npm 软件包作为替代。别名仅在你的项目中生效,不会重命名传递依赖中的软件包。别名应遵循 validate-npm-package-name 中规定的命名约定。
示例:
npm install my-react@npm:reactnpm install jquery2@npm:jquery@2npm install jquery3@npm:jquery@3npm install npa@npm:npm-package-arg
npm install [<@scope>/]<name>@<tag>:
安装由指定标签引用的软件包版本。如果该标签在该软件包的注册表数据中不存在,则操作将失败。
示例:
npm install sax@latestnpm install @myorg/mypackage@latest
npm install [<@scope>/]<name>@<version>:
安装指定版本的包。如果该版本尚未发布到注册表,将会失败。
示例:
npm install sax@0.1.1npm install @myorg/privatepackage@1.5.0
npm install [<@scope>/]<name>@<version range>:
安装与指定版本范围匹配的包版本。这将遵循 package.json 中描述的相同依赖解析规则。
请注意,大多数版本范围必须放在引号中,以便你的 shell 将其视为单个参数。
示例:
npm install sax@">=0.1.0 <0.2.0"npm install @myorg/privatepackage@"16 - 17"
npm install <git remote url>:
从托管的 git 提供程序安装包,使用 git 克隆它。对于完整的 git 远程 URL,将只尝试该 URL。
<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
<protocol> 是 git、git+ssh、git+http、git+https 或 git+file 之一。
如果提供了 #<commit-ish>,它将被用来克隆该提交。如果 commit-ish 的格式为 #semver:<semver>,<semver> 可以是任何有效的 semver 范围或确切版本,npm 会在远程仓库中查找与该范围匹配的任何标签或引用,就像对注册表依赖一样。如果未指定 #<commit-ish> 或 #semver:<semver>,则使用仓库的默认分支。
如果存储库使用子模块,这些子模块也将被克隆。
如果正在安装的软件包包含 prepare 脚本,那么在打包和安装该软件包之前,其 dependencies 和 devDependencies 将被安装,并且准备脚本将被执行。
以下 git 环境变量被 npm 识别,并在运行 git 时添加到环境中:
GIT_ASKPASSGIT_EXEC_PATHGIT_PROXY_COMMANDGIT_SSHGIT_SSH_COMMANDGIT_SSL_CAINFOGIT_SSL_NO_VERIFY有关详细信息,请参阅 git 手册页。
示例:
npm install git+ssh://git@github.com:npm/cli.git#v1.0.27npm install git+ssh://git@github.com:npm/cli#pull/273npm install git+ssh://git@github.com:npm/cli#semver:^5.0npm install git+https://isaacs@github.com/npm/cli.gitnpm install git://github.com/npm/cli.git#v1.0.27GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://git@github.com:npm/cli.git
npm install <githubname>/<githubrepo>[#<commit-ish>]:
npm install github:<githubname>/<githubrepo>[#<commit-ish>]:
尝试使用 git 克隆位于 https://github.com/githubname/githubrepo 的软件包以进行安装。
如果提供了 #<commit-ish>,将使用它精确克隆该提交。如果 commit-ish 的格式为 #semver:<semver>,<semver> 可以是任何有效的 semver 范围或确切版本,npm 将在远程仓库中查找与该范围匹配的任何标签或引用,就像处理注册表依赖一样。如果未指定 #<commit-ish> 或 #semver:<semver>,则使用默认分支。
与常规的 git 依赖一样,如果包在安装完成之前有 prepare 脚本,dependencies 和 devDependencies 将会被安装。
示例:
npm install mygithubuser/myprojectnpm install github:mygithubuser/myproject
npm install gist:[<githubname>/]<gistID>[#<commit-ish>|#semver:<semver>]:
通过尝试使用 git 克隆它,在 https://gist.github.com/gistID 安装该软件包。与该 gist 关联的 GitHub 用户名是可选的,并且不会保存在 package.json 中。
与常规的 git 依赖一样,如果包在安装完成之前有 prepare 脚本,dependencies 和 devDependencies 将会被安装。
示例:
npm install gist:101a11beef
npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]:
通过尝试使用 git 克隆它来在 https://bitbucket.org/bitbucketname/bitbucketrepo 安装该包。
如果提供了 #<commit-ish>,它将被用来克隆该特定提交。如果 commit-ish 的格式为 #semver:<semver>,<semver> 可以是任何有效的语义版本范围或精确版本,npm 将在远程仓库中查找匹配该范围的任何标签或引用,就像处理注册表依赖一样。如果既没有指定 #<commit-ish> 也没有指定 #semver:<semver>,那么将使用 master。
与常规的 git 依赖一样,如果包在安装完成之前有 prepare 脚本,dependencies 和 devDependencies 将会被安装。
示例:
npm install bitbucket:mybitbucketuser/myproject
npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]:
通过尝试使用 git 克隆它来在 https://gitlab.com/gitlabname/gitlabrepo 安装该包。
如果提供了 #<commit-ish>,它将被用来克隆该特定提交。如果 commit-ish 的格式为 #semver:<semver>,<semver> 可以是任何有效的语义版本范围或精确版本,npm 将在远程仓库中查找匹配该范围的任何标签或引用,就像处理注册表依赖一样。如果既没有指定 #<commit-ish> 也没有指定 #semver:<semver>,那么将使用 master。
与常规的 git 依赖一样,如果包在安装完成之前有 prepare 脚本,dependencies 和 devDependencies 将会被安装。
示例:
npm install gitlab:mygitlabuser/myprojectnpm install gitlab:myusr/myproj#semver:^5.0
你可以结合多个参数,甚至多种类型的参数。例如:
🌐 You may combine multiple arguments and even multiple types of arguments. For example:
npm install sax@">=0.1.0 <0.2.0" bench supervisor
--tag 参数将适用于所有指定的安装目标。如果存在具有给定名称的标签,则优先使用带标签的版本,而不是较新的版本。
🌐 The --tag argument will apply to all of the specified install targets. If a tag with the given name exists, the tagged version is preferred over newer versions.
--dry-run 参数会以通常的方式报告安装将会做什么,但不会实际安装任何东西。
🌐 The --dry-run argument will report in the usual way what the install would have done without actually installing anything.
--package-lock-only 参数只会更新 package-lock.json,而不会检查 node_modules 也不会下载依赖。
🌐 The --package-lock-only argument will only update the package-lock.json, instead of checking node_modules and downloading dependencies.
-f 或 --force 参数将强制 npm 获取远程资源,即使本地已有副本。
🌐 The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk.
npm install sax --force
🌐 Configuration
请参阅 config 帮助文档。许多配置参数对安装有一定影响,因为 npm 的主要功能就是安装。
🌐 See the config help doc. Many of the configuration params have some effect on installation, since that's most of what npm does.
这些是与安装相关的一些最常见的选项。
🌐 These are some of the most common options related to installation.
savetrue,除非使用 npm update 时,默认值为 false将已安装的软件包保存到 package.json 文件中作为依赖。
🌐 Save installed packages to a package.json file as dependencies.
与 npm rm 命令一起使用时,会移除对 package.json 的依赖。
🌐 When used with the npm rm command, removes the dependency from package.json.
如果设置为 false,也将阻止向 package-lock.json 写入。
🌐 Will also prevent writing to package-lock.json if set to false.
save-exact保存到 package.json 的依赖将使用精确的版本进行配置,而不是使用 npm 的默认 semver 范围运算符。
🌐 Dependencies saved to package.json will be configured with an exact version rather than using npm's default semver range operator.
global以“全局”模式运行,因此软件包会安装到 prefix 文件夹中,而不是当前工作目录。有关行为差异的更多信息,请参见 folders。
🌐 Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.
{prefix}/lib/node_modules 文件夹中,而不是当前工作目录。{prefix}/bin 关联{prefix}/share/maninstall-strategy设置在 node_modules 中安装包的策略。hoisted(默认):在顶层安装非重复包,必要时在目录结构中安装重复包。nested(原先为 --legacy-bundling):就地安装,不采用提升。shallow(原先为 --global-style):仅在顶层安装直接依赖。linked(实验性):安装在 node_modules/.store 中,就地链接,不提升。
🌐 Sets the strategy for installing packages in node_modules. hoisted (default): Install non-duplicated in top-level, and duplicated as necessary within directory structure. nested: (formerly --legacy-bundling) install in place, no hoisting. shallow (formerly --global-style) only install direct deps at top-level. linked: (experimental) install in node_modules/.store, link in place, unhoisted.
legacy-bundling--install-strategy=nested不要在 node_modules 中提升包安装,而是按照依赖的方式安装包。这可能会导致非常深的目录结构和重复的包安装,因为没有去重功能。设置 --install-strategy=nested。
🌐 Instead of hoisting package installs in node_modules, install packages in the same manner that they are depended on. This may cause very deep directory structures and duplicate package installs as there is no de-duplicating. Sets --install-strategy=nested.
global-style--install-strategy=shallow 取代只在顶层 node_modules 安装直接依赖,但在更深层的依赖上使用提升(hoist)。设置 --install-strategy=shallow。
🌐 Only install direct dependencies in the top level node_modules, but hoist on deeper dependencies. Sets --install-strategy=shallow.
omitNODE_ENV 环境变量设置为 'production',则为 'dev',否则为空。要从磁盘上的安装树中省略的依赖类型。
🌐 Dependency types to omit from the installation tree on disk.
请注意,这些依赖仍然会被解析并添加到 package-lock.json 或 npm-shrinkwrap.json 文件中。它们只是没有实际安装到磁盘上。
🌐 Note that these dependencies are still resolved and added to the package-lock.json or npm-shrinkwrap.json file. They are just not physically installed on disk.
如果某种封装类型同时出现在 --include 和 --omit 列表中,那么它将被包括在内。
🌐 If a package type appears in both the --include and --omit lists, then it will be included.
如果生成的省略列表包含 'dev',那么所有生命周期脚本的 NODE_ENV 环境变量将被设置为 'production'。
🌐 If the resulting omit list includes 'dev', then the NODE_ENV environment variable will be set to 'production' for all lifecycle scripts.
strict-peer-deps如果设置为 true,且未设置 --legacy-peer-deps,那么任何冲突的 peerDependencies 都将被视为安装失败,即使 npm 能够基于非 peer 依赖合理地猜测适当的解决方案。
🌐 If set to true, and --legacy-peer-deps is not set, then any conflicting peerDependencies will be treated as an install failure, even if npm could reasonably guess the appropriate resolution based on non-peer dependency relationships.
默认情况下,依赖图中深层的冲突 peerDependencies 将使用最近的非对等依赖规范来解决,即使这样做会导致某些包获得的对等依赖超出其包的 peerDependencies 对象中设置的范围。
🌐 By default, conflicting peerDependencies deep in the dependency graph will be resolved using the nearest non-peer dependency specification, even if doing so will result in some packages receiving a peer dependency outside the range set in their package's peerDependencies object.
当执行此类覆盖时,会打印警告,解释冲突及涉及的包。如果设置了 --strict-peer-deps,则此警告将被视为失败。
🌐 When such an override is performed, a warning is printed, explaining the conflict and the packages involved. If --strict-peer-deps is set, then this warning is treated as a failure.
prefer-dedupe如果可能,最好对包进行数据去重,而不是选择更新版本的依赖。
🌐 Prefer to deduplicate packages if possible, rather than choosing a newer version of a dependency.
package-lock如果设置为 false,则在安装时忽略 package-lock.json 文件。如果 save 为 true,这也将防止写入 package-lock.json。
🌐 If set to false, then ignore package-lock.json files when installing. This will also prevent writing package-lock.json if save is true.
package-lock-only如果设置为 true,当前操作将只使用 package-lock.json,忽略 node_modules。
🌐 If set to true, the current operation will only use the package-lock.json, ignoring node_modules.
对于 update,这意味着只会更新 package-lock.json,而不会检查 node_modules 或下载依赖。
🌐 For update this means only the package-lock.json will be updated, instead of checking node_modules and downloading dependencies.
对于 list,这意味着输出将基于 package-lock.json 描述的树,而不是 node_modules 的内容。
🌐 For list this means the output will be based on the tree described by the package-lock.json, rather than the contents of node_modules.
foreground-scripts在前台进程中运行已安装包的所有构建脚本(即 preinstall、install 和 postinstall 脚本),并与主 npm 进程共享标准输入、输出和错误。
🌐 Run all build scripts (ie, preinstall, install, and postinstall) scripts for installed packages in the foreground process, sharing standard input, output, and error with the main npm process.
请注意,这通常会使安装运行速度变慢,并且噪音更大,但对调试很有用。
🌐 Note that this will generally make installs run slower, and be much noisier, but can be useful for debugging.
ignore-scripts如果为 true,npm 不会运行 package.json 文件中指定的脚本。
🌐 If true, npm does not run scripts specified in package.json files.
请注意,明确用于运行特定脚本的命令,例如 npm start、npm stop、npm restart、npm test 和 npm run-script,如果设置了 ignore-scripts,仍会运行其指定的脚本,但它们不会运行任何前置或后置脚本。
🌐 Note that commands explicitly intended to run a particular script, such as npm start, npm stop, npm restart, npm test, and npm run-script will still run their intended script if ignore-scripts is set, but they will not run any pre- or post-scripts.
audit当设置为“true”时,会将审核报告与当前的 npm 命令一起提交到默认注册表以及为作用域配置的所有注册表。有关提交内容的详细信息,请参阅 npm audit 文档。
🌐 When "true" submit audit reports alongside the current npm command to the default registry and all registries configured for scopes. See the documentation for npm audit for details on what is submitted.
bin-links告诉 npm 为包的可执行文件创建符号链接(在 Windows 上则创建 .cmd 垫片)。
🌐 Tells npm to create symlinks (or .cmd shims on Windows) for package executables.
设置为 false 可使其不执行此操作。这可以用来应对某些文件系统不支持符号链接的情况,即使在表面上的 Unix 系统上也是如此。
🌐 Set to false to have it not do this. This can be used to work around the fact that some file systems don't support symlinks, even on ostensibly Unix systems.
fund当“true”显示消息时,会在每个 npm install 的末尾确认正在寻找资金的依赖数量。详情请参见 npm fund。
🌐 When "true" displays the message at the end of each npm install acknowledging the number of dependencies looking for funding. See npm fund for details.
dry-run表示你不希望 npm 做出任何更改,并且它只应该报告本来会做的事情。这个选项可以传递给任何修改本地安装的命令,例如 install、update、dedupe、uninstall,以及 pack 和 publish。
🌐 Indicates that you don't want npm to make any changes and that it should only report what it would have done. This can be passed into any of the commands that modify your local installation, eg, install, update, dedupe, uninstall, as well as pack and publish.
注意:其他与网络相关的命令(例如 dist-tags、owner 等)不支持此项。
🌐 Note: This is NOT honored by other network related commands, eg dist-tags, owner, etc.
cpu覆盖本地模块的 CPU 架构以进行安装。可接受的值与 package.json 的 cpu 字段相同,该字段来自 process.arch。
🌐 Override CPU architecture of native modules to install. Acceptable values are same as cpu field of package.json, which comes from process.arch.
os覆盖本地模块的操作系统以进行安装。可接受的值与 package.json 的 os 字段相同,该字段来自 process.platform。
🌐 Override OS of native modules to install. Acceptable values are same as os field of package.json, which comes from process.platform.
workspace启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
🌐 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.
workspace 配置的有效值为以下之一:
🌐 Valid values for the workspace config are either:
对于 npm init 命令设置时,可以将其设置为一个尚不存在的工作区文件夹,以创建该文件夹并将其作为项目内全新的工作区进行设置。
🌐 When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
workspaces设置为 true 以在 所有 配置的工作区上下文中运行命令。
🌐 Set to true to run the command in the context of all configured workspaces.
将此显式设置为 false 会导致像 install 这样的命令完全忽略工作区。如果不显式设置:
🌐 Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:
node_modules 树操作的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。- 执行其他操作的命令(测试、执行、发布等)会在根项目上运行,除非 在 workspace 配置中指定了一个或多个工作区。此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
include-workspace-root为命令启用工作区时包括工作区根。
🌐 Include the workspace root when workspaces are enabled for a command.
当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上操作,而不会在根项目上操作。
🌐 When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
install-links当设置文件时:协议依赖将作为常规依赖被打包和安装,而不会创建符号链接。此选项对工作区没有影响。
🌐 When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.
🌐 Algorithm
给定一个 package{dep} 结构:A{B,C}, B{C}, C{D},npm 安装算法产生如下结果:
🌐 Given a package{dep} structure: A{B,C}, B{C}, C{D}, the npm install algorithm produces:
A+-- B+-- C+-- D
也就是说,从 B 到 C 的依赖得以满足,因为 A 已经导致 C 在更高级别被安装。D 仍然安装在顶层,因为没有任何东西与它冲突。
🌐 That is, the dependency from B to C is satisfied by the fact that A already caused C to be installed at a higher level. D is still installed at the top level because nothing conflicts with it.
对于 A{B,C}, B{C,D@1}, C{D@2},这个算法生成:
🌐 For A{B,C}, B{C,D@1}, C{D@2}, this algorithm produces:
A+-- B+-- C`-- D@2+-- D@1
因为 B 的 D@1 将安装在顶层,C 现在必须为自己私下安装 D@2。这个算法是确定性的,但如果两个依赖按不同的顺序被请求安装,可能会生成不同的依赖树。
🌐 Because B's D@1 will be installed in the top-level, C now has to install D@2 privately for itself. This algorithm is deterministic, but different trees may be produced if two dependencies are requested for installation in a different order.
请参阅 folders 以获取有关 npm 创建的特定文件夹结构的更详细说明。
🌐 See folders for a more detailed description of the specific folder structures that npm creates.
🌐 See Also