npmrc

npm 配置文件

选择命令行版本:

描述

🌐 Description

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.

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

🌐 For a list of available configuration options, see config.

文件

🌐 Files

四个相关文件是:

🌐 The four relevant files are:

  • 每个项目的配置文件(/path/to/my/project/.npmrc)
  • 每个用户的配置文件 (~/.npmrc)
  • 全局配置文件 ($PREFIX/etc/npmrc)
  • npm 内置配置文件 (/path/to/npm/npmrc)

所有 npm 配置文件都是以 ini 格式列出的 key = value 参数列表。环境变量可以使用 ${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"

注释

🌐 Comments

.npmrc 文件中的行以 ;# 字符开头时,这些行会被解释为注释。.npmrc 文件由 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
@myscope:registry=https://mycustomregistry.example.org

每个项目的配置文件

🌐 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 参数,则使用 userconfig

全局配置文件

🌐 Global config file

$PREFIX/etc/npmrc(或者上面设置的 globalconfig 参数):此文件是一个以 ini 文件格式列出的 key = value 参数列表。环境变量可以像上面那样进行替换。

内置配置文件

🌐 Built-in config file

path/to/npm/itself/npmrc

这是一个不可更改的“内置”配置文件,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 认证字符串)
  • _authToken(认证令牌)
  • username
  • _password
  • email
  • certfile(证书文件路径)
  • keyfile(密钥文件路径)

为了范围化这些值,它们必须以 URI 片段作为前缀。如果凭证适用于单个主机上对注册表的任何请求,范围可能看起来像 //registry.npmjs.org/:。如果必须将其限定在主机上的特定路径,该路径也可以提供,例如 //my-custom-registry.org/unique/path:

🌐 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 //registry.npmjs.org/:. If it must be scoped to a specific path on the host that path may also be provided, such as //my-custom-registry.org/unique/path:.

; bad config
_authToken=MYTOKEN
; good config
@myorg:registry=https://somewhere-else.com/myorg
@another:registry=https://somewhere-else.com/another
//registry.npmjs.org/:_authToken=MYTOKEN
; would apply to both @myorg and @another
; //somewhere-else.com/:_authToken=MYTOKEN
; would apply only to @myorg
//somewhere-else.com/myorg/:_authToken=MYTOKEN1
; would apply only to @another
//somewhere-else.com/another/:_authToken=MYTOKEN2

也可以看看

🌐 See also