选择命令行版本:
🌐 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)/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}. By default if the variable is not defined, it is left unreplaced. By adding ? after variable name they can be forced to evaluate to an empty string instead. For example:
cache = ${HOME}/.npm-packagesnode-options = "${NODE_OPTIONS?} --use-system-ca"
这些文件中的每一个都会被加载,并按优先顺序解析配置选项。例如,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_modules 和 package.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、_authToken、username、_password、certfile 和 keyfile 必须都限定到特定的注册表。这可以确保 npm 永远不会向错误的主机发送凭据。
🌐 The settings _auth, _authToken, username, _password, certfile, and keyfile 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_passwordemailcafile(证书授权文件的路径)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:.
🌐 Unsupported Custom Configuration Keys
从 npm v11.2.0 开始,当在 .npmrc 中定义未知的配置键时,npm 会发出警告。在未来的 npm 主要版本中,这些未知的键可能不再被接受。
🌐 Starting in npm v11.2.0, npm warns when unknown configuration keys are defined in .npmrc. In a future major version of npm, these unknown keys may no longer be accepted.
只有 npm 官方支持的配置键才会被识别。用于第三方工具的自定义键(例如 electron-builder)不应放在 .npmrc 中。
🌐 Only configuration keys that npm officially supports are recognized. Custom keys intended for third-party tools (for example, electron-builder) should not be placed in .npmrc.
如果你需要在脚本中使用包级别的配置,请在你的 package.json 中使用 config 字段,如下所示:
🌐 If you need package-level configuration for use in scripts, use the config field in your package.json instead:
{"name": "my-package","config": {"mirror": "https://example.com/"}}
package.json#config 中定义的值会作为带有 npm_package_config_ 前缀的环境变量暴露给脚本。例如:
🌐 Values defined in package.json#config are exposed to scripts as environment variables prefixed with npm_package_config_. For example:
npm_package_config_mirror
如果你需要向脚本命令传递参数,使用 -- 来分隔 npm 参数和脚本参数:
🌐 If you need to pass arguments to a script command, use -- to separate npm arguments from script arguments:
npm run build -- --customFlag
建议使用环境变量进行跨平台配置,而不是在 .npmrc 中定义不受支持的键。
🌐 Using environment variables is also recommended for cross-platform configuration instead of defining unsupported keys in .npmrc.
; 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
🌐 Custom / third-party config keys
npm 只识别它自己的配置选项。如果你的 .npmrc 包含不属于 npm 配置定义的键(例如 electron_mirror 或 sass_binary_site),npm 会发出警告:
🌐 npm only recognizes its own configuration options. If your .npmrc contains keys that are not part of npm's config definitions (for example, electron_mirror or sass_binary_site), npm will emit a warning:
warn Unknown user config "electron_mirror".This will stop working in the next major version of npm.
这些键在历史上是被容忍的,但官方不支持。npm 的未来主要版本将把未知的顶层键视为错误。
🌐 These keys were historically tolerated but are not officially supported. A future major version of npm will treat unknown top-level keys as errors.
一些工具(例如 @electron/get 或 node-sass)根据约定从环境变量或 .npmrc 读取自身的配置。你可以将这些值设置为环境变量来替代:
🌐 Some tools (such as @electron/get or node-sass) read their own configuration from environment variables or from .npmrc by convention. You can set these values as environment variables instead:
export ELECTRON_MIRROR="https://mirrorexample.npmjs.org/mirrors/electron/"export ELECTRON_CUSTOM_DIR="{{ version }}"
环境变量是最具可移植性的方法,无论 .npmrc 格式如何都能使用。
🌐 Environment variables are the most portable approach and work regardless of .npmrc format.
🌐 See also