npm-install
选择命令行版本:
See Details
目录
概要
¥Synopsis
npm install [<package-spec> ...]aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
描述
¥Description
此命令安装一个包和它所依赖的任何包。如果包有一个包锁,或者一个 npm 收缩封装文件,或者一个 Yarn 锁文件,依赖的安装将由它驱动,遵循以下优先顺序:
¥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.json
-
package-lock.json
-
yarn.lock
见 package-lock.json 和 npm shrinkwrap
。
¥See package-lock.json and npm shrinkwrap
.
一个 package
是:
¥A package
is:
-
a) 包含由
package.json
文件描述的程序的文件夹¥a) a folder containing a program described by a
package.json
file -
b) 一个 gzipped tarball,包含 (a)
¥b) a gzipped tarball containing (a)
-
c) 解析为 (b) 的 url
¥c) a url that resolves to (b)
-
d) 在注册表上发布的
<name>@<version>
(参见registry
),带有 (c)¥d) a
<name>@<version>
that is published on the registry (seeregistry
) with (c) -
e) 指向 (d) 的
<name>@<tag>
(见npm dist-tag
)¥e) a
<name>@<tag>
(seenpm dist-tag
) that points to (d) -
f) 具有满足 (e) 的 "latest" 标签的
<name>
¥f) a
<name>
that has a "latest" tag satisfying (e) -
g) 解决为 (a) 的
<git remote url>
¥g) a
<git remote url>
that resolves to (a)
即使你从不发布你的包,如果你只是想写一个 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
(在包目录中,无参数):¥
npm install
(in a package directory, no arguments):将依赖安装到本地
node_modules
文件夹。¥Install the dependencies to the local
node_modules
folder.在全局模式下(即,将
-g
或--global
附加到命令中),它将当前包上下文(即当前工作目录)安装为全局包。¥In global mode (ie, with
-g
or--global
appended to the command), it installs the current package context (ie, the current working directory) as a global package.默认情况下,
npm install
将安装package.json
中列为依赖的所有模块。¥By default,
npm install
will install all modules listed as dependencies inpackage.json
.使用
--production
标志(或者当NODE_ENV
环境变量设置为production
时),npm 将不会安装devDependencies
中列出的模块。当NODE_ENV
环境变量设置为production
时,要安装dependencies
和devDependencies
中列出的所有模块,你可以使用--production=false
。¥With the
--production
flag (or when theNODE_ENV
environment variable is set toproduction
), npm will not install modules listed indevDependencies
. To install all modules listed in bothdependencies
anddevDependencies
whenNODE_ENV
environment variable is set toproduction
, you can use--production=false
.注意:在向项目添加依赖时,
--production
标志没有特殊含义。¥NOTE: The
--production
flag has no particular meaning when adding a dependency to a project. -
npm install <folder>
:如果
<folder>
位于项目的根目录中,它的依赖将被安装,并且可能会像其他类型的依赖一样被提升到顶层node_modules
。如果<folder>
位于项目根目录之外,npm 不会在目录<folder>
中安装包依赖,但会创建到<folder>
的符号链接。¥If
<folder>
sits inside the root of your project, its dependencies will be installed and may be hoisted to the top-levelnode_modules
as they would for other types of dependencies. If<folder>
sits outside the root of your project, npm will not install the package dependencies in the directory<folder>
, but it will create a symlink to<folder>
.注意:如果你想从注册表安装目录的内容(如包)而不是创建链接,则需要使用
--install-links
选项。¥NOTE: If you want to install the content of a directory like a package from the registry instead of creating a link, you would need to use the
--install-links
option.示例:
¥Example:
npm install ../../other-package --install-linksnpm install ./sub-package -
npm install <tarball file>
:安装位于文件系统上的包。注意:如果你只想将一个 dev 目录链接到你的 npm 根目录,你可以使用
npm link
更容易地做到这一点。¥Install a package that is sitting on the filesystem. Note: if you just want to link a dev directory into your npm root, you can do this more easily by using
npm link
.压缩包要求:
¥Tarball requirements:
-
文件名必须使用
.tar
、.tar.gz
或.tgz
作为扩展名。¥The filename must use
.tar
,.tar.gz
, or.tgz
as the extension. -
包内容应位于 tarball 内的子文件夹中(通常称为
package/
)。npm 在安装包时剥离一个目录层(运行tar x --strip-components=1
的等效项)。¥The package contents should reside in a subfolder inside the tarball (usually it is called
package/
). npm strips one directory layer when installing the package (an equivalent oftar x --strip-components=1
is run). -
包必须包含具有
name
和version
属性的package.json
文件。¥The package must contain a
package.json
file withname
andversion
properties.
示例:
¥Example:
npm install ./package.tgz -
-
npm install <tarball url>
:获取 tarball url,然后安装它。为了区分此选项和其他选项,参数必须以 "http://" 或 "https://" 开头
¥Fetch the tarball url, and then install it. In order to distinguish between this and other options, the argument must start with "http://" or "https://"
示例:
¥Example:
npm install https://github.com/indexzero/forever/tarball/v0.5.6 -
npm install [<@scope>/]<name>
:进行
<name>@<tag>
安装,其中<tag>
是 "tag" 配置。(参见config
。配置的默认值为latest
。)¥Do a
<name>@<tag>
install, where<tag>
is the "tag" config. (Seeconfig
. The config's default value islatest
.)在大多数情况下,这将在 npm 注册表上安装标记为
latest
的模块版本。¥In most cases, this will install the version of the modules tagged as
latest
on the npm registry.示例:
¥Example:
npm install sax默认情况下,
npm install
将任何指定的包保存到dependencies
中。此外,你可以使用一些额外的标志来控制它们的保存位置和方式:¥
npm install
saves any specified packages intodependencies
by default. Additionally, you can control where and how they get saved with some additional flags:-
-P, --save-prod
:包将出现在你的dependencies
中。这是默认值,除非存在-D
或-O
。¥
-P, --save-prod
: Package will appear in yourdependencies
. This is the default unless-D
or-O
are present. -
-D, --save-dev
:包将出现在你的devDependencies
中。¥
-D, --save-dev
: Package will appear in yourdevDependencies
. -
--save-peer
:包将出现在你的peerDependencies
中。¥
--save-peer
: Package will appear in yourpeerDependencies
. -
-O, --save-optional
:包将出现在你的optionalDependencies
中。¥
-O, --save-optional
: Package will appear in youroptionalDependencies
. -
--no-save
:防止保存到dependencies
。¥
--no-save
: Prevents saving todependencies
.
当使用上述任何选项将依赖保存到 package.json 时,还有两个额外的可选标志:
¥When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:
-
-E, --save-exact
:保存的依赖将使用精确的版本进行配置,而不是使用 npm 的默认 semver 范围运算符。¥
-E, --save-exact
: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator. -
-B, --save-bundle
:保存的依赖也将添加到你的bundleDependencies
列表中。¥
-B, --save-bundle
: Saved dependencies will also be added to yourbundleDependencies
list.
此外,如果你有
npm-shrinkwrap.json
或package-lock.json
,那么它也会被更新。¥Further, if you have an
npm-shrinkwrap.json
orpackage-lock.json
then it will be updated as well.<scope>
是可选的。该包将从与指定范围关联的注册表中下载。如果没有注册表与给定范围关联,则假定为默认注册表。见scope
。¥
<scope>
is optional. The package will be downloaded from the registry associated with the specified scope. If no registry is associated with the given scope the default registry is assumed. Seescope
.注意:如果你不在范围名称中包含 @ 符号,npm 会将其解释为 GitHub 存储库,请参见下文。范围名称后还必须跟一个斜杠。
¥Note: if you do not include the @-symbol on your scope name, npm will interpret this as a GitHub repository instead, see below. Scopes names must also be followed by a slash.
-