npm 配置文件

选择 CLI 版本:



npm 从命令行、环境变量和 npmrc 文件获取其配置设置。

npm gets its config settings from the command line, environment variables, and npmrc files.

npm config 命令可用于更新和编辑用户和全局 npmrc 文件的内容。

The npm config command can be used to update and edit the contents of the user and global npmrc files.

有关可用配置选项的列表,请参阅 配置

For a list of available configuration options, see config.




The four relevant files are:

  • per-project config file (/path/to/my/project/.npmrc)

  • per-user config file (~/.npmrc)

  • global config file ($PREFIX/etc/npmrc)

  • npm 内置配置文件 (/path/to/npm/npmrc)

    npm builtin config file (/path/to/npm/npmrc)

所有 npm 配置文件都是 key = value 参数的 ini 格式列表。可以使用 ${VARIABLE_NAME} 替换环境变量。例如:

All npm config files are an ini-formatted list of key = value parameters. Environment variables can be replaced using ${VARIABLE_NAME}. For example:

prefix = ${HOME}/.npm-packages

这些文件中的每一个都已加载,并且配置选项按优先级顺序解析。例如,userconfig 文件中的设置将覆盖 globalconfig 文件中的设置。

Each of these files is loaded, and config options are resolved in priority order. For example, a setting in the userconfig file would override the setting in the globalconfig file.

通过在键名后添加 "[]" 来指定数组值。例如:

Array values are specified by adding "[]" after the key name. For example:

key[] = "first value"
key[] = "second value"



.npmrc 文件中的行以 ;# 字符开头时被解释为注释。.npmrc 文件由 npm/ini 解析,npm/ini 指定此注释语法。

Lines in .npmrc files are interpreted as comments when they begin with a ; or # character. .npmrc files are parsed by npm/ini, which specifies this comment syntax.


For example:

# last modified: 01 Jan 2016
; Set a new registry for a scoped package


Per-project config file

在项目中本地工作时,项目根目录中的 .npmrc 文件(即 node_modulespackage.json 的兄弟)将设置特定于该项目的配置值。

When working locally in a project, a .npmrc file in the root of the project (ie, a sibling of node_modules and package.json) will set config values specific to this project.

请注意,这仅适用于你正在运行 npm 的项目的根目录。当你的模块发布时,它不起作用。例如,你不能发布强制自身全局安装或安装在不同位置的模块。

Note that this only applies to the root of the project that you're running npm in. It has no effect when your module is published. For example, you can't publish a module that forces itself to install globally, or in a different location.

此外,在全局模式下不会读取此文件,例如在运行 npm install -g 时。

Additionally, this file is not read in global mode, such as when running npm install -g.


Per-user config file

$HOME/.npmrc(或 userconfig 参数,如果在环境或命令行中设置)

$HOME/.npmrc (or the userconfig param, if set in the environment or on the command line)


Global config file

$PREFIX/etc/npmrc(或 globalconfig 参数,如果在上面设置):此文件是 key = value 参数的 ini 文件格式列表。可以如上所述替换环境变量。

$PREFIX/etc/npmrc (or the globalconfig param, if set above): This file is an ini-file formatted list of key = value parameters. Environment variables can be replaced as above.


Built-in config file


这是一个不可更改的 "builtin" 配置文件,npm 在更新时保持一致。使用 npm 附带的 ./configure 脚本在此处设置字段。这主要是为了分发维护者以标准和一致的方式覆盖默认配置。

This is an unchangeable "builtin" configuration file that npm keeps consistent across updates. Set fields in here using the ./configure script that comes with npm. This is primarily for distribution maintainers to override default configs in a standard and consistent manner.


Auth related configuration

设置 _auth_authTokenusername_password 都必须限定在特定注册表范围内。这可确保 npm 永远不会将凭据发送到错误的主机。

The settings _auth, _authToken, username and _password must all be scoped to a specific registry. This ensures that npm will never send credentials to the wrong host.


The full list is:

  • _auth(base64 认证字符串)

    _auth (base64 authentication string)

  • _authToken(身份验证令牌)

    _authToken (authentication token)

  • username

  • _password

  • email

  • certfile(证书文件的路径)

    certfile (path to certificate file)

  • keyfile(密钥文件的路径)

    keyfile (path to key file)

为了限定这些值的范围,它们必须以 URI 片段为前缀。如果凭证用于对单个主机上的注册表的任何请求,则范围可能类似于 //。如果必须将其范围限定为主机上的特定路径,则还可以提供该路径,例如 //

In order to scope these values, they must be prefixed by a URI fragment. If the credential is meant for any request to a registry on a single host, the scope may look like // If it must be scoped to a specific path on the host that path may also be provided, such as //

; bad config
; good config
; would apply to both @myorg and @another
; //
; would apply only to @myorg
; would apply only to @another


See also

npm 中文网 - 粤ICP备13048890号