npm 經常使用配置

官方文檔:docs.npmjs.com/misc/confightml

npm 配置來源

npm 從如下來源獲取配置信息(優先級由高到低):node

命令行選項

  • 在命令行中用 --foo bar 設置配置參數 foo 的值爲 "bar"
  • CLI 解析器碰到 -- 時中止讀取配置參數值。
  • 使用 --foo 而不指定任何值時,設置配置參數 foo 的值爲 true

示例:shell

# 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 文件(優先級由高到低):npm

  • 項目配置文件(/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 默認配置參數。json

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

若是指定的配置參數縮寫能夠無歧義地解析爲一個已知的配置參數,它將會被解析爲該配置參數,示例:bash

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 配置覆蓋參數值。示例:ui

{
    "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…url

常見配置參數:spa

  • 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 lsnpm 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_PROXYhttp_proxy 環境變量,底層請求庫將使用代理設置。

  • registry

    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]
複製代碼
相關文章
相關標籤/搜索