配置
选择命令行版本:
See Details
目录
描述
¥Description
本文详细介绍了 npm 一般配置。要了解 config 命令,请参阅 npm config。
¥This article details npm configuration in general. To learn about the config command, see npm config.
npm 从以下来源获取其配置值,按优先级排序:
¥npm gets its configuration values from the following sources, sorted by priority:
命令行标志
¥Command Line Flags
将 --foo bar 放在命令行上会将 foo 配置参数设置为 "bar"。-- 参数告诉 cli 解析器停止读取标志。在不指定任何值的情况下使用 --flag 会将值设置为 true。
¥Putting --foo bar on the command line sets the foo configuration parameter to "bar". A -- argument tells the cli parser to stop reading flags. Using --flag without specifying any value will set the value to true.
示例:--flag1 --flag2 会将两个配置参数设置为 true,而 --flag1 --flag2 bar 会将 flag1 设置为 true,将 flag2 设置为 bar。最后,--flag1 --flag2 -- bar 将两个配置参数都设置为 true,并将 bar 作为命令参数。
¥Example: --flag1 --flag2 will set both configuration parameters to true, while --flag1 --flag2 bar will set flag1 to true, and flag2 to bar. Finally, --flag1 --flag2 -- bar will set both configuration parameters to true, and the bar is taken as a command argument.
环境变量
¥Environment Variables
任何以 npm_config_ 开头的环境变量都将被解释为配置参数。例如,将 npm_config_foo=bar 放入你的环境中会将 foo 配置参数设置为 bar。任何未赋值的环境配置都将被赋值为 true。配置值不区分大小写,因此 NPM_CONFIG_FOO=bar 的工作方式相同。但是,请注意,在 scripts 内部,npm 将设置自己的环境变量,并且 Node 会更喜欢那些小写版本,而不是你可能设置的任何大写版本。详情见 此问题。
¥Any environment variables that start with npm_config_ will be interpreted as a configuration parameter. For example, putting npm_config_foo=bar in your environment will set the foo configuration parameter to bar. Any environment configurations that are not given a value will be given the value of true. Config values are case-insensitive, so NPM_CONFIG_FOO=bar will work the same. However, please note that inside scripts npm will set its own environment variables and Node will prefer those lowercase versions over any uppercase ones that you might set. For details see this issue.
请注意,你需要使用下划线而不是破折号,因此 --allow-same-version 将变为 npm_config_allow_same_version=true。
¥Notice that you need to use underscores instead of dashes, so --allow-same-version would become npm_config_allow_same_version=true.
npmrc 文件
¥npmrc Files
四个相关文件是:
¥The four relevant files are:
- 
每个项目的配置文件 (
/path/to/my/project/.npmrc)¥per-project configuration file (
/path/to/my/project/.npmrc) - 
每用户配置文件(默认为
$HOME/.npmrc;可通过 CLI 选项--userconfig或环境变量$NPM_CONFIG_USERCONFIG配置)¥per-user configuration file (defaults to
$HOME/.npmrc; configurable via CLI option--userconfigor environment variable$NPM_CONFIG_USERCONFIG) - 
全局配置文件(默认为
$PREFIX/etc/npmrc;可通过 CLI 选项--globalconfig或环境变量$NPM_CONFIG_GLOBALCONFIG配置)¥global configuration file (defaults to
$PREFIX/etc/npmrc; configurable via CLI option--globalconfigor environment variable$NPM_CONFIG_GLOBALCONFIG) - 
npm 的内置配置文件(
/path/to/npm/npmrc)¥npm's built-in configuration file (
/path/to/npm/npmrc) 
有关详细信息,请参阅 npmrc。
¥See npmrc for more details.
默认配置
¥Default Configs
运行 npm config ls -l 以查看一组 npm 内部的配置参数,如果未指定其他参数,则为默认值。
¥Run npm config ls -l to see a set of configuration parameters that are internal to npm, and are defaults if nothing else is specified.
速记和其他 CLI 细节
¥Shorthands and Other CLI Niceties
在命令行上解析以下简写:
¥The following shorthands are parsed on the command-line:
- 
-a:--all - 
--enjoy-by:--before - 
-c:--call - 
--desc:--description - 
-f:--force - 
-g:--global - 
--iwr:--include-workspace-root - 
-L:--location - 
-d:--loglevel info - 
-s:--loglevel silent - 
--silent:--loglevel silent - 
--ddd:--loglevel silly - 
--dd:--loglevel verbose - 
--verbose:--loglevel verbose - 
-q:--loglevel warn - 
--quiet:--loglevel warn - 
-l:--long - 
-m:--message - 
--local:--no-global - 
-n:--no-yes - 
--no:--no-yes - 
-p:--parseable - 
--porcelain:--parseable - 
-C:--prefix - 
--readonly:--read-only - 
--reg:--registry - 
-S:--save - 
-B:--save-bundle - 
-D:--save-dev - 
-E:--save-exact - 
-O:--save-optional - 
-P:--save-prod - 
-?:--usage - 
-h:--usage - 
-H:--usage - 
--help:--usage - 
-v:--version - 
-w:--workspace - 
--ws:--workspaces - 
-y:--yes 
如果指定的配置参数明确地解析为已知的配置参数,则将其扩展为该配置参数。例如:
¥If the specified configuration param resolves unambiguously to a known configuration parameter, then it is expanded to that configuration parameter. For example:
npm ls --par# same as:npm ls --parseable
如果多个单字符速记串在一起,并且生成的组合明确地不是某 个其他配置参数,那么它会扩展为它的各个组成部分。例如:
¥If multiple single-character shorthands are strung together, and the resulting combination is unambiguously not some other configuration param, then it is expanded to its various component pieces. For example:
npm ls -gpld# same as:npm ls --global --parseable --long --loglevel info
配置设置
¥Config Settings
_auth
- 
默认值:null
¥Default: null
 - 
类型:空值或字符串
¥Type: null or String
 
对 npm 注册表进行身份验证时使用的基本身份验证字符串。这将仅用于对 npm 注册表进行身份验证。对于其他注册表,你需要像 "//other-registry.tld/:_auth" 一样对其进行范围
¥A basic-auth string to use when authenticating against the npm registry. This will ONLY be used to authenticate against the npm registry. For other registries you will need to scope it like "//other-registry.tld/:_auth"
警告:这通常不应通过命令行选项设置。通过运行 npm login 使用存储在 ~/.npmrc 文件中的注册表提供的身份验证承载令牌更安全。
¥Warning: This should generally not be set via a command-line option. It is safer to use a registry-provided authentication bearer token stored in the ~/.npmrc file by running npm login.
access
- 
默认值:'public' 用于新包,现有包不会改变当前级别
¥Default: 'public' for new packages, existing packages it will not change the current level
 - 
类型:空值、"restricted" 或 "public"
¥Type: null, "restricted", or "public"
 
如果你不希望你的范围包公开可见(和可安装),请设置 --access=restricted。
¥If you do not want your scoped package to be publicly viewable (and installable) set --access=restricted.
无范围的包不能设置为 restricted。
¥Unscoped packages can not be set to restricted.
注意:这默认为不更改现有包的当前访问级别。在发布期间指定值 restricted 或 public 将更改对现有包的访问权限,就像 npm access set status 一样。
¥Note: This defaults to not changing the current access level for existing packages. Specifying a value of restricted or public during publish will change the access for an existing package the same way that npm access set status would.
all
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
运行 npm outdated 和 npm ls 时,设置 --all 将显示所有过时或已安装的包,而不仅仅是当前项目直接依赖的包。
¥When running npm outdated and npm ls, setting --all will show all outdated or installed packages, rather than only those directly depended upon by the current project.
allow-same-version
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
防止在使用 npm version 将新版本设置为与当前版本相同的值时引发错误。
¥Prevents throwing an error when npm version is used to set the new version to the same value as the current version.
audit
- 
默认值:true
¥Default: true
 - 
类型:布尔值
¥Type: Boolean
 
当 "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.
audit-level
- 
默认值:null
¥Default: null
 - 
类型:空、"info"、"low"、"moderate"、"high"、"critical" 或 "none"
¥Type: null, "info", "low", "moderate", "high", "critical", or "none"
 
npm audit 以非零退出代码退出的最低漏洞级别。
¥The minimum level of vulnerability for npm audit to exit with a non-zero exit code.
auth-type
- 
默认值:"web"
¥Default: "web"
 - 
类型:"legacy" 或 "web"
¥Type: "legacy" or "web"
 
login 使用什么身份验证策略。请注意,如果给出 otp 配置,则此值将始终设置为 legacy。
¥What authentication strategy to use with login. Note that if an otp config is given, this value will always be set to legacy.
before
- 
默认值:null
¥Default: null
 - 
类型:空或日期
¥Type: null or Date
 
如果传递给 npm install,将重建 npm 树,以便仅安装在指定日期或之前可用的版本。如果当前依赖集没有可用的版本,则命令将出错。
¥If passed to npm install, will rebuild the npm tree such that only versions that were available on or before the given date are installed. If there are no versions available for the current set of dependencies, the command will error.
如果请求的版本是 dist-tag 并且给定的标签没有通过 --before 过滤器,则将使用小于或等于该标签的最新版本。例如,foo@latest 可能会安装 foo@1.2,即使 latest 是 2.0。
¥If the requested version is a dist-tag and the given tag does not pass the --before filter, the most recent version less than or equal to that tag will be used. For example, foo@latest might install foo@1.2 even though latest is 2.0.
bin-links
- 
默认值:true
¥Default: true
 - 
类型:布尔值
¥Type: Boolean
 
告诉 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.
browser
- 
默认值:macOS:
"open",Windows:"start"、其他:"xdg-open"¥Default: macOS:
"open", Windows:"start", Others:"xdg-open" - 
类型:空值、布尔值或字符串
¥Type: null, Boolean, or String
 
npm 命令调用以打开网站的浏览器。
¥The browser that is called by npm commands to open websites.
设置为 false 以抑制浏览器行为,而是将 url 打印到终端。
¥Set to false to suppress browser behavior and instead print urls to terminal.
设置为 true 以使用默认系统 URL 打开器。
¥Set to true to use default system URL opener.
ca
- 
默认值:null
¥Default: null
 - 
类型:null 或 String(可设置多次)
¥Type: null or String (can be set multiple times)
 
受信任的证书颁发机构签名证书,可用于与注册表的 SSL 连接。值应采用 PEM 格式(Windows 将其称为 "Base-64 编码的 X.509 (.CER)"),并用字符串 "\n" 替换换行符。例如:
¥The Certificate Authority signing certificate that is trusted for SSL connections to the registry. Values should be in PEM format (Windows calls it "Base-64 encoded X.509 (.CER)") with newlines replaced by the string "\n". For example:
ca="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
设置为 null 仅允许 "known" 注册商,或设置为特定 CA 证书以仅信任该特定签名机构。
¥Set to null to only allow "known" registrars, or to a specific CA cert to trust only that specific signing authority.
通过指定证书数组可以信任多个 CA:
¥Multiple CAs can be trusted by specifying an array of certificates:
ca[]="..."ca[]="..."
另请参阅 strict-ssl 配置。
¥See also the strict-ssl config.
cache
- 
默认值:Windows:
%LocalAppData%\npm-cache,Posix:~/.npm¥Default: Windows:
%LocalAppData%\npm-cache, Posix:~/.npm - 
类型:路径
¥Type: Path
 
npm 的缓存目录的位置。
¥The location of npm's cache directory.
cafile
- 
默认值:null
¥Default: null
 - 
类型:路径
¥Type: Path
 
包含一个或多个证书颁发机构签名证书的文件的路径。类似于 ca 设置,但允许多个 CA,以及将 CA 信息存储在磁盘上的文件中。
¥A path to a file containing one or multiple Certificate Authority signing certificates. Similar to the ca setting, but allows for multiple CA's, as well as for the CA information to be stored in a file on disk.
call
- 
默认值:""
¥Default: ""
 - 
类型:字符串
¥Type: String
 
npm exec、npx 的可选配套选项,允许指定自定义命令与已安装的包一起运行。
¥Optional companion option for npm exec, npx that allows for specifying a custom command to be run along with the installed packages.
npm exec --package yo --package generator-node --call "yo node"
cidr
- 
默认值:null
¥Default: null
 - 
类型:null 或 String(可设置多次)
¥Type: null or String (can be set multiple times)
 
这是使用 npm token create 命令配置受限访问令牌时要使用的 CIDR 地址列表。
¥This is a list of CIDR address to be used when configuring limited access tokens with the npm token create command.
color
- 
默认值:true 除非 NO_COLOR 环境设置为 '0' 以外的值
¥Default: true unless the NO_COLOR environ is set to something other than '0'
 - 
类型:"always" 或布尔值
¥Type: "always" or Boolean
 
如果为 false,则从不显示颜色。如果 "always" 则始终显示颜色。如果为真,则仅打印 tty 文件描述符的颜色代码。
¥If false, never shows colors. If "always" then always shows colors. If true, then only prints color codes for tty file descriptors.
commit-hooks
- 
默认值:true
¥Default: true
 - 
类型:布尔值
¥Type: Boolean
 
使用 npm version 命令时运行 git commit hooks。
¥Run git commit hooks when using the npm version command.
cpu
- 
默认值:null
¥Default: null
 - 
类型:空值或字符串
¥Type: null or String
 
覆盖要安装的原生模块的 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.
depth
- 
默认值:如果设置了
--all,则为Infinity,否则为0¥Default:
Infinityif--allis set, otherwise0 - 
类型:空值或数字
¥Type: null or Number
 
为 npm ls 递归包时的深度。
¥The depth to go when recursing packages for npm ls.
如果未设置,npm ls 将仅显示根项目的直接依赖。如果设置了 --all,那么 npm 将默认显示所有依赖。
¥If not set, npm ls will show only the immediate dependencies of the root project. If --all is set, then npm will show all dependencies by default.
description
- 
默认值:true
¥Default: true
 - 
类型:布尔值
¥Type: Boolean
 
显示 npm search 中的描述
¥Show the description in npm search
diff
- 
默认值:
¥Default:
 - 
类型:字符串(可以设置多次)
¥Type: String (can be set multiple times)
 
定义要在 npm diff 中比较的参数。
¥Define arguments to compare in npm diff.
diff-dst-prefix
- 
默认值:"b/"
¥Default: "b/"
 - 
类型:字符串
¥Type: String
 
npm diff 输出中使用的目标前缀。
¥Destination prefix to be used in npm diff output.
diff-ignore-all-space
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
比较 npm diff 中的行时忽略空格。
¥Ignore whitespace when comparing lines in npm diff.
diff-name-only
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
使用 npm diff 时仅打印文件名。
¥Prints only filenames when using npm diff.
diff-no-prefix
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
不要在 npm diff 输出中显示任何源或目标前缀。
¥Do not show any source or destination prefix in npm diff output.
注意:这会导致 npm diff 忽略 --diff-src-prefix 和 --diff-dst-prefix 配置。
¥Note: this causes npm diff to ignore the --diff-src-prefix and --diff-dst-prefix configs.
diff-src-prefix
- 
默认值:"a/"
¥Default: "a/"
 - 
类型:字符串
¥Type: String
 
npm diff 输出中使用的源前缀。
¥Source prefix to be used in npm diff output.
diff-text
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
将所有文件视为 npm diff 中的文本。
¥Treat all files as text in npm diff.
diff-unified
- 
默认值:3
¥Default: 3
 - 
类型:数字
¥Type: Number
 
要在 npm diff 中打印的上下文行数。
¥The number of lines of context to print in npm diff.
dry-run
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
表示你不希望 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.
editor
- 
默认值:EDITOR 或 VISUAL 环境变量,或 Windows 上的 '%SYSTEMROOT%\notepad.exe',或 Unix 系统上的 'vi'
¥Default: The EDITOR or VISUAL environment variables, or '%SYSTEMROOT%\notepad.exe' on Windows, or 'vi' on Unix systems
 - 
类型:字符串
¥Type: String
 
为 npm edit 和 npm config edit 运行的命令。
¥The command to run for npm edit and npm config edit.
engine-strict
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
如果设置为 true,那么 npm 将顽固地拒绝安装(甚至考虑安装)任何声称与当前 Node.js 版本不兼容的包。
¥If set to true, then npm will stubbornly refuse to install (or even consider installing) any package that claims to not be compatible with the current Node.js version.
这可以通过设置 --force 标志来覆盖。
¥This can be overridden by setting the --force flag.
expect-result-count
- 
默认值:null
¥Default: null
 - 
类型:空值或数字
¥Type: null or Number
 
告知期望从命令中得到特定数量的结果。
¥Tells to expect a specific number of results from the command.
此配置不能用于:expect-results
¥This config can not be used with: expect-results
expect-results
- 
默认值:null
¥Default: null
 - 
类型:空值或布尔值
¥Type: null or Boolean
 
告诉 npm 是否期望该命令的结果。可以为 true(期望一些结果)或 false(期望没有结果)。
¥Tells npm whether or not to expect results from the command. Can be either true (expect some results) or false (expect no results).
此配置不能用于:expect-result-count
¥This config can not be used with: expect-result-count
fetch-retries
- 
默认值:2
¥Default: 2
 - 
类型:数字
¥Type: Number
 
从注册表获取包时要使用的 retry 模块的 "retries" 配置。
¥The "retries" config for the retry module to use when fetching packages from the registry.
在网络故障或 5xx HTTP 错误的情况下,npm 将重试对注册表的幂等读取请求。
¥npm will retry idempotent read requests to the registry in the case of network failures or 5xx HTTP errors.
fetch-retry-factor
- 
默认值:10
¥Default: 10
 - 
类型:数字
¥Type: Number
 
获取包时要使用的 retry 模块的 "factor" 配置。
¥The "factor" config for the retry module to use when fetching packages.
fetch-retry-maxtimeout
- 
默认值:60000(1 分钟)
¥Default: 60000 (1 minute)
 - 
类型:数字
¥Type: Number
 
获取包时要使用的 retry 模块的 "maxTimeout" 配置。
¥The "maxTimeout" config for the retry module to use when fetching packages.
fetch-retry-mintimeout
- 
默认值:10000(10 秒)
¥Default: 10000 (10 seconds)
 - 
类型:数字
¥Type: Number
 
获取包时要使用的 retry 模块的 "minTimeout" 配置。
¥The "minTimeout" config for the retry module to use when fetching packages.
fetch-timeout
- 
默认值:300000(5 分钟)
¥Default: 300000 (5 minutes)
 - 
类型:数字
¥Type: Number
 
等待 HTTP 请求完成的最长时间。
¥The maximum amount of time to wait for HTTP requests to complete.
force
- 
默认值:false
¥Default: false
 - 
类型:布尔值
¥Type: Boolean
 
删除了针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。
¥Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
- 
允许在全局安装中破坏非 npm 文件。
¥Allow clobbering non-npm files in global installs.
 - 
允许
npm version命令在不干净的 git 存储库上工作。¥Allow the
npm versioncommand to work on an unclean git repository. - 
允许使用
npm cache clean删除缓存文件夹。¥Allow deleting the cache folder with
npm cache clean. - 
允许安装具有
engines声明需要不同版本的 npm 的包。¥Allow installing packages that have an
enginesdeclaration requiring a different version of npm. - 
允许安装具有
engines声明需要不同版本node的包,即使启用了--engine-strict。¥Allow installing packages that have an
enginesdeclaration requiring a different version ofnode, even if--engine-strictis enabled. - 
允许
npm audit fix安装超出你声明的依赖范围的模块(包括 SemVer 的主要更改)。¥Allow
npm audit fixto install modules outside your stated dependency range (including SemVer-major changes). - 
允许取消发布已发布包的所有版本。
¥Allow unpublishing all versions of a published package.
 - 
允许在根项目中安装冲突的 peerDependencies。
¥Allow conflicting peerDependencies to be installed in the root project.
 - 
在
npm init时隐式设置--yes。¥Implicitly set
--yesduringnpm init. - 
允许破坏
npm pkg中的现有值¥Allow clobbering existing values in
npm pkg - 
允许取消发布整个包(不仅仅是单个版本)。
¥Allow unpublishing of entire packages (not just a single version).
 
如果你对自己想要做什么没有明确的想法,强烈建议你不要使用此选项!
¥If you don't have a clear idea of what you want to do, it is strongly recommended that you do not use this option!
foreground-scripts
- 
默认值:
false除非使用npm pack或npm publish时默认为true¥Default:
falseunless when usingnpm packornpm publishwhere it defaults totrue - 
类型:布尔值
¥Type: Boolean