npm 常用配置
官方文檔:docs.npmjs.com/misc/config
npm 配置來源
npm 從以下來源獲取配置信息(優先級由高到低):
命令行選項
- 在命令行中用 --foo bar 設置配置參數 foo 的值為 "bar"。
- CLI 解析器碰到 -- 時停止讀取配置參數值。
- 使用 --foo 而不指定任何值時,設置配置參數 foo 的值為 true。
示例:
# flag1 -> true, flag2 -> true --flag1 --flag2# flag1 -> true, flag2 -> "bar" --flag1 --flag2 bar# flag1 -> true, flag2 -> true, bar 為命令參數,不是配置參數 flag2 的值 --flag1 --flag2 -- bar 復制代碼環境變量
- 帶有 npm_config_ 前綴的環境變量會被解釋為 npm 配置參數。例如,環境變量中的npm_config_foo=bar 將會設置配置參數 foo 的值為 "bar"。
- 只指定參數名卻沒有指定任何值的配置參數,其值將會被設置為 true。
npmrc 文件
有如下四類 npmrc 文件(優先級由高到低):
- 項目配置文件(/path/to/my/project/.npmrc)
- 用戶配置文件(默認為 $HOME/.npmrc/;可通過 CLI 選項 --userconfig 或環境變量 $NPM_CONFIG_USERCONFIG 指定)
- 全局配置文件(默認為 $PREFIX/etc/npmrc;可通過 CLI 選項 --globalconfig 或環境變量 $NPM_CONFIG_GLOBALCONFIG 指定)
- npm 內置的配置文件(/path/to/npm/npmrc)
默認配置
執行 npm config ls -l 查看 npm 配置參數,如果沒有額外指定配置,則該命令的輸出結果是 npm 默認配置參數。
CLI 參數縮寫
- -v: --version
- -h, -?, --help, -H: --usage
- -s, --silent: --loglevel silent
- -q, --quiet: --loglevel warn
- -d: --loglevel info
- -dd, --verbose: --loglevel verbose
- -ddd: --loglevel silly
- -g: --global
- -C: --prefix
- -l: --long
- -m: --message
- -p, --porcelain: --parseable
- -reg: --registry
- -f: --force
- -desc: --description
- -S: --save
- -P: --save-prod
- -D: --save-dev
- -O: --save-optional
- -B: --save-bundle
- -E: --save-exact
- -y: --yes
- -n: --yes false
- ll and la commands: ls --long
如果指定的配置參數縮寫可以無歧義地解析為一個已知的配置參數,它將會被解析為該配置參數,示例:
npm ls --par # same as: npm ls --parseable 復制代碼如果多個單字母縮寫的配置參數連在一起,并且結果無法無歧義地解析為一個已知配置參數,那么這些連在一起的縮寫參數會被解析為這些配置參數的組合,示例:
npm ls -gpld # same as: npm ls --global --parseable --long --loglevel info 復制代碼Per-Package Config Settings
執行 npm-scripts 時,若腳本中引用了 package.json 文件中的 config 字段下的配置參數,用戶可通過 npm 配置覆蓋參數值。示例:
{"name": "foo","config": {"port": "8080"},"scripts": {"start": "node server.js"} } 復制代碼// server.js http.createServer(...).listen(process.env.npm_config_port) 復制代碼# 通過配置覆蓋 package.json 中 config.port 的值 npm config set foo:port 80 復制代碼配置參數
所有配置參數:docs.npmjs.com/misc/config…
常見配置參數:
-
cache
- Default: Windows:%AppData%\npm-cache,Posix:~/.npm
- Type: path
npm-cache
-
color
- Default: true
- type: Boolean 或 "always"
如果為 false,永遠不彩色打印,如果為 "always",永遠彩色打印,如果為 true,只彩色打印 tty file descriptors。
當環境變量 NO_COLOR 設置為任何值時,禁用彩色打印。
-
depth
- Default: Infinity
- Type: Number
執行 npm ls, npm cache ls, npm outdated 命令時,遞歸目錄的深度。
對于 npm outdated 時,Infinity 將會被視為 0。想要檢查多有包及其依賴包,需要使用一個大整數,例如:npm outdated --depth 9999。
-
dry-run
- Default: false
- Type: Boolean
若設置為 true,npm 只報告要做什么,而不會付諸行動。
-
global
- Default: false
- Type: Boolean
若設置為 true,在 "global" 模式下執行命令:
- npm 包會被安裝到 {prefix}/lib/node_modules 目錄而不是當前工作目錄。
- bin 文件鏈接到 {prefix}/bin 目錄
- man pages 鏈接到 {prefix}/share/man 目錄
-
globalconfig
- Default: {prefix}/et/npmrc
- Type: path
-
https_proxy
- Default: null
- Type: url
-
long
- Default: false
- Type: Boolean
執行 npm ls 和 npm search 打印詳細信息。
-
node-version
- Default: precess.version
- Type: semver or false
檢查 package.json 中的 engines 字段時使用的 node 版本號。
-
onload-script
- Default: false
- Type: path
npm 啟動時 require() 的腳本。
-
optional
- Default: false
- Type: Boolean
嘗試安裝 optionalDependencies 對象中的包,如果這些包安裝失敗,不阻礙其他包的安裝。
-
package-lock
- Default: true
- Type: Boolean
如果設為 false,在 npm install 時忽略 package-lock.json。
當 package-locks 被禁用時,多余模塊的自動裁剪也會被禁用。此時,移除多余模塊要使用 npm prune 命令
-
parseable
- Default: false
- Type: Boolean
讓執行的命令輸出可解析的結果。對于 npm search,指定該選項將會以 tab 分隔的格式輸出結果。
-
prefix
- Default: 與 node 安裝路徑有關,node 安裝在 {prefix}/bin 目錄下。
- Type: path
全局包的安裝位置。如果再命令行中指定,則在指定目錄執行非全局命令。
-
production
- Default: false
- Type: Boolean
設置為 true 時:
- 不帶參數執行 npm install 時,不會在項目的 node_modules 目錄的的直接子目錄中安裝 devDependencies 中的包。
- 為生命周期鉤子腳本設置環境變量:NODE_ENV=production。
-
progress
- Default: true, 除非已通過 TRAVIS 或 CI 環境變量設置.
- Type: Boolean
是否為時間敏感的操作顯示進度條。
-
proxy
- Default: null
- Type: url
如果設置了 HTTP_PROXY 或 http_proxy 環境變量,底層請求庫將使用代理設置。
-
registry
- Default: registry.npmjs.org/
- Type: url
npm 源地址。
-
rollback
- Default: true
- Type: Boolean
是否移除安裝失敗的包。
-
save
- Default: true
- Type: Boolean
只在當前目錄中存在 package.json 文件時有效。執行 npm install ... 時,將安裝的包加入 dependencies,執行 npm rm ... 時將刪除的包從 dependencies 中移除。
-
save-dev
- Default: false
- Type: Boolean
類似 save 參數,相應的 package.json 字段:devDependencies。
-
save-optional
- Default: false
- Type: Boolean
類似 save 參數,相應的 package.json 字段:optionalDependencies。
-
save-exact
- Default: false
- Type: Boolean
如果設置為 true,使用 --save,--save-dev 或 --save-optional 保存到 package.json 中的依賴包的會被配置為確定的版本,而不使用 npm 默認的語義化版本范圍符號(semver range operator)。
-
save-prefix
- Default: ^
- Type: String
為使用 --save,--save-dev 或 --save-optional 安裝的依賴包指定版本號前綴。
例如,如果依賴包的版本為 1.2.3,在 package.json 中該包的版本會被設置為 ^1.2.3,這允許使用該包的 minor upgrades。如果將 save-prefix 設置為 ~,在 package.json 中該包的版本會被設置為 ~1.2.3,這允許使用該包的 patch upgrades。
-
script-shell
- Default: null
- Type: path
通過 npm run 命令執行腳本時使用的 shell.
-
searchlimit
- Default: 20
- Type: Number
npm search ... 結果數上限。
-
shell
- Default: SHELL environment variable, or "bash" on Posix, or "cmd" on Windows
- Type: path
執行 npm explore 命令時使用的 shell。
-
userconfig
- Default: ~/.npmrc
- Type: path
-
version
- Default: false
- Type: boolean
如果設為 true,輸出 npm 版本。
-
versions
- Default: false
- Type: boolean
如果設為 true,輸出 npm 和 node 進程相關的版本信息。
使用 CLI 命令設置和讀取配置參數
npm config(alias:npm c) 命令可以用來更新和編輯用戶和全局 npmrc 文件。
# 設置配置參數 key 的值為 value,如果省略 value,key 會被設置為 true npm config set <key> <value> [-g|--global]# 查看配置參數 key 的值 npm config get <key># 刪除配置參數 key npm config delete <key># 查看所有設置過的配置參數。使用 -l 查看所有設置過的以及默認的配置參數。使用 --json 以 json 格式查看。 npm config list [-l] [--json]# 在編輯器中打開 npmrc 文件,使用 --global 參數打開全局 npmrc 文件。 npm config edit# 同 npm config get <key> npm get <key># 同 npm config set <key> <value> [-g|--global] npm set <key> <value> [-g|--global] 復制代碼轉載于:https://juejin.im/post/5c9653c7f265da60f96f8e3e
總結
- 上一篇: [JVM-3]Java垃圾回收(GC)机
- 下一篇: 刚刚开源的Python静态类型检查器:P