.npmrc
pnpm 從命令列、環境變數和 .npmrc
檔中取得組態。
pnpm config
命令可以用於更新或編輯使用者及全域 .npmrc
檔的內容。
四個相關的檔案是:
- 每個專案的設定檔 (
/path/to/my/project/.npmrc
) - 每個工作區的設定檔 (包含
pnpm-workspace.yaml
檔的目錄) - 每位使用者的設定檔 (
~/.npmrc
) - 全域設定檔 (
/etc/npmrc
)
所有 .npmrc
檔都是使用 INI 格式的 key = value
參數清單。
相依性提升設定
hoist
- 預設值:true
- 類型:boolean
若為 true
,則提升所有依賴包提升到 node_modules/.pnpm
。 這樣使得未列出的依賴可以被 node_modules
中的所有包訪問。
hoist-pattern
- 預設值:['*']
- 類型:string[]
告訴 pnpm 應該將哪些包提升到 node_modules/.pnpm
。 在預設情況下,所有的包都是被提升的,但是如果你知道只有一些有缺陷的包有幻影依賴關係, 您可以使用此選項來只提升有幻影依賴關係的包(推薦)。
例如:
hoist-pattern[]=*eslint*
hoist-pattern[]=*babel*
自 v7.12.0 起,您可能還需要使用 !
來從提升中排除模式。
例如:
hoist-pattern[]=*types*
hoist-pattern[]=!@types/react
public-hoist-pattern
- 預設值:['*eslint*', '*prettier*']
- 類型:string[]
與 hoist-pattern
不同,public-hoist-pattern
不會將相依性提升至虛擬存放區內部隱藏的模塊目錄,而是將符合樣式的相依性提升至根模塊目錄。 提升至根模組目錄表示應用程式程式碼將可以存取虛設的相依性,即使這個程式碼對解決方案策略進行了不當的修改也一樣。
在處理一些不當解析相依性的錯誤可插入式工具時,這個設定很有用。
例如:
public-hoist-pattern[]=*plugin*
注意:將 shamefully-hoist
設定為 true
或將 public-hoist-pattern
設定為 *
是一樣的。
自 v7.12.0 起,您可能還需要使用 !
來從提升中排除模式。
例如:
public-hoist-pattern[]=*types*
public-hoist-pattern[]=!@types/react
shamefully-hoist
- 預設值:false
- 類型:Boolean
預設情況下,pnpm 會建立一個半嚴格式的 node_modules
資料夾,表示相依性可以存取為宣告的相依性,但 node_modules
外的模組不可以。 使用此頁面配置,生態系統中的大多數套件將沒有問題的運作。 不過,如果一些工具僅在提升的相依性位於 node_modules
的根目錄時才會運作,您可以將其設定為 true
來提升它們。
節點模組設定
store-dir
- 預設值:
- If the $PNPM_HOME env variable is set, then $PNPM_HOME/store
- If the $XDG_DATA_HOME env variable is set, then $XDG_DATA_HOME/pnpm/store
- On Windows: ~/AppData/Local/pnpm/store
- On macOS: ~/Library/pnpm/store
- On Linux: ~/.local/share/pnpm/store
- 類型:path
所有套件儲存在磁碟上的位置。
存放區應該一律安裝在相同的磁碟上,因此每個磁碟都將有一 個存放區。 如果目前的磁碟上有主目錄,則將在其內部建立儲存區。 如果磁碟上沒有主目錄,則將在檔案系統的根目錄中建立儲存區。 例如,如果安裝是發生在裝在 /mnt
中的檔案系統中,則將會在 /mnt/.pnpm-store
中建立存放區。 Windows 系統也是這樣。
從不同的磁碟設定存放區是可行的,但在那種情況下,pnpm 將不會建立指向套件的永久連結,而是會從存放區複製,因為只有在相同的檔案系統上才可以建立永久連結。
modules-dir
- 預設值:node_modules
- 類型:path
將在其中安裝相依性的目錄 (而非 node_modules
)。
node-linker
- 預設值:isolated
- 類型:isolated, hoisted, pnp
定義應該使用哪些連結器來安裝 Node 套件。
- isolated - 從位於
node_modules/.pnpm
的虛擬存放區建立指向相依性的符號連結。 - hoisted - 沒有建立符號連結的一般
node_modules
。 與 npm 或 Yarn 傳統版建立的node_modules
相同。 使用這個設定時,有一個 Yarn 程式庫用於提升。 使用此設定的合法原因:- 您的工具無法與符號連結一起使用。 React Native 專案將很有可能僅在您使用已提升的
node_modules
時才可以使用。 - 您的專案部署在一個無伺服器的主機服務提供者。 一些無伺服器的提供者 (例如 AWS Lambia) 不支援符號連結。 或者,您也可以在部署之前組合應用程式來解決此問題。
- 您想要使用
"bundledDependencies"
發布套件。 - 您正在使用 --preserve-symlinks 旗標執行 Node.js。
- 您的工具無法與符號連結一起使用。 React Native 專案將很有可能僅在您使用已提升的
- pnp - 無
node_modules
。 隨插即用是一種創新策略,適用於 Yarn Berry 使用的 Node。 推薦在使用pnp
作為連結器時,也將symlink
設定為false
。
symlink
- 預設值:true
- 類型:Boolean
當 symlink
設定為 false
時,pnpm 會建立一個不含任何符號連結的虛擬存放區目錄。 這個設定與 node-linker=pnp
同樣是有用的。
enable-modules-dir
- 預設值:true
- 類型:Boolean
若為 false
,pnpm 將不會將任何檔案寫入至模組目錄 (node_modules
)。 這對於模組目錄同使用者空間內檔案系統 (FUSE) 裝載時有幫助。 有一個實驗性 CLI 允許您使用 FUSE 裝載模組目錄:@pnpm/mount-modules。
virtual-store-dir
- 預設值:node_modules/.pnpm
- 類型:path
包含指向存放區連結的目錄。 專案所有直接和間接依賴的依賴套件都建立了指向此目錄的連結。
這個設定對解決 Windows 上長路徑的問題有幫助。 如果某些相依套件的路徑很長,您可以在磁碟機的根目錄 (例如,C:\my-project-store
) 中選取虛擬存放區。
或者,您可以將虛擬存放區設定至 .pnpm
並將其新增至 .gitignore
。 這將會讓 StrackTrace 更加簡潔,因為相依套件的路徑將會高出一個目錄。
**注意:**虛擬存放區無法在多個專案間共用。 每個專案都應該有自己的虛擬存放區 (共用根的工作區內除外)。
package-import-method
- 預設值:auto
- 類型: auto, hardlink, copy, clone, clone-or-copy
Controls the way packages are imported from the store (if you want to disable symlinks inside node_modules
, then you need to change the node-linker setting, not this one).
- auto - 嘗試從存放區複製套件。 如果不支援複製,則從存放區建立指向套的 Hardlink。 如果再製和建立連結都不可行,則回到複製
- hardlink - 從存放區建立套件的永久連結
- clone-or-copy - 嘗試從存放區再製套件。 如果不支援再製,則回到複製
- copy - 從存放區複製套件
- clone - 從存放區再製 (又稱為寫入時複製或參考連結) 套件
再製是將套件寫入 node_modules 的最佳方式。 這是最快速、最安全的方式。 使用再製時,您可以在 node_modules 中編輯檔案,而在中央的內容可定址存放區中,它們將不會被修改。
不過,不是所有的檔案系統都支援再製。 我們推薦使用寫入時複製 (CoW) 檔案系統 (例如,在 Linux 上使用 Btrfs 而非 Ext4),以取得使用 pnpm 的最佳體驗。
Even though macOS supports cloning, there is currently a bug in Node.js that prevents us from using it in pnpm. If you have ideas how to fix it, help us.
modules-cache-max-age
- 預設值: 10080 (7 天,以分鐘為單位)
- 類型:Number
模組目錄中的孤立套件應該被移除 的時間 (單位為分鐘)。 pnpm 會在模組目錄中保留套件的快取。 這會提高切換分支或降級相依性時的安裝速度。
鎖定檔設定
lockfile
- 預設值:true
- 類型:Boolean
若設定為 false
,pnpm 將不會讀取或產生 pnpm-lock.yaml
檔。
prefer-frozen-lockfile
- 預設值: true
- 類型:Boolean
若設定為 true
且可用的 pnpm-lock.yaml
符合 package.json
的相依性指示詞,則執行無周邊安裝。 無周邊安裝會略過所有的相依性解析,因為它不需要修改鎖定檔。
lockfile-include-tarball-url
新增於:v7.6.0
- 預設值:false
- 類型:Boolean
將指向套件 tarball 的完整 URL 新增至 pnpm-lock.yaml
中的每個項目。
git-branch-lockfile
Added in: v7.3.0
- 預設值:false
- 類型:Boolean
When set to true
, the generated lockfile name after installation will be named based on the current branch name to completely avoid merge conflicts. For example, if the current branch name is feature-foo
, the corresponding lockfile name will be pnpm-lock.feature-foo.yaml
instead of pnpm-lock.yaml
. It is typically used in conjunction with the command line argument --merge-git-branch-lockfiles
or by setting merge-git-branch-lockfiles-branch-pattern
in the .npmrc
file.
merge-git-branch-lockfiles-branch-pattern
Added in: v7.3.0
- Default: null
- Type: Array or null
This configuration matches the current branch name to determine whether to merge all git branch lockfile files. By default, you need to manually pass the --merge-git-branch-lockfiles
command line parameter. This configuration allows this process to be automatically completed.
例如:
merge-git-branch-lockfiles-branch-pattern[]=main
merge-git-branch-lockfiles-branch-pattern[]=release*
You may also exclude patterns using !
.
use-lockfile-v6
Added in: v7.24.0
- 預設值:false
- 類型:Boolean
Use the new v6 lockfile format, which will be the default one in pnpm v8. This new format is more readable as it doesn't use hashes to shorten long dependency paths.
登錄與驗證設定
registry
- Default: https://registry.npmjs.org/
- Type: url
The base URL of the npm package registry (trailing slash included).
<scope>:registry
The npm registry that should be used for packages of the specified scope. For example, setting @babel:registry=https://example.com/packages/npm/
will enforce that when you use pnpm add @babel/core
, or any @babel
scoped package, the package will be fetched from https://example.com/packages/npm
instead of the default registry.
<URL>:_authToken
Define the authentication bearer token to use when accessing the specified registry. 範例:
//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
You may also use an environment variable. 範例:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Or you may just use an environment variable directly, without changing .npmrc
at all:
npm_config_//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
<URL>:tokenHelper
A token helper is an executable which outputs an auth token. This can be used in situations where the authToken is not a constant value but is something that refreshes regularly, where a script or other tool can use an existing refresh token to obtain a new access token.
The configuration for the path to the helper must be an absolute path, with no arguments. In order to be secure, it is only permitted to set this value in the user .npmrc
. Otherwise a project could place a value in a project's local .npmrc
and run arbitrary executables.
Setting a token helper for the default registry:
tokenHelper=/home/ivan/token-generator
Setting a token helper for the specified registry:
//registry.corp.com:tokenHelper=/home/ivan/token-generator
Request Settings
ca
- Default: The npm CA certificate
- Type: String, Array or null
The Certificate Authority signing certificate that is trusted for SSL connections to the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). 範例:
ca="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
Set to null to only allow known registrars, or to a specific CA cert to trust only that specific signing authority.
Multiple CAs can be trusted by specifying an array of certificates:
ca[]="..."
ca[]="..."
See also the strict-ssl
config.
cafile
- Default: null
- 類型:path
A path to a file containing one or multiple Certificate Authority signing certificates. Similar to the ca
setting, but allows for multiple CAs, as well as for the CA information to be stored in a file instead of being specified via CLI.
cert
- Default: null
- Type: String
A client certificate to pass when accessing the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). 範例:
cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
It is not the path to a certificate file (and there is no certfile
option).
key
- Default: null
- Type: String
A client key to pass when accessing the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). 範例:
key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----"
It is not the path to a key file (and there is no keyfile
option).
This setting contains sensitive information. Don't write it to a local .npmrc
file committed to the repository.
git-shallow-hosts
- Default: ['github.com', 'gist.github.com', 'gitlab.com', 'bitbucket.com', 'bitbucket.org']
- 類型:string[]
When fetching dependencies that are Git repositories, if the host is listed in this setting, pnpm will use shallow cloning to fetch only the needed commit, not all the history.
https-proxy
- Default: null
- Type: url
A proxy to use for outgoing HTTPS requests. If the HTTPS_PROXY
, https_proxy
, HTTP_PROXY
or http_proxy
environment variables are set, their values will be used instead.
If your proxy URL contains a username and password, make sure to URL-encode them. 例如:
https-proxy=https://use%21r:pas%2As@my.proxy:1234/foo
Do not encode the colon (:
) between the username and password.
http-proxy
proxy
- Default: null
- Type: url
A proxy to use for outgoing http requests. If the HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library.
local-address
- Default: undefined
- Type: IP Address
The IP address of the local interface to use when making connections to the npm registry.
maxsockets
- Default: network-concurrency x 3
- Type: Number
The maximum number of connections to use per origin (protocol/host/port combination).
noproxy
- Default: null
- Type: String
A comma-separated string of domain extensions that a proxy should not be used for.
strict-ssl
- 預設值:true
- 類型:Boolean
Whether or not to do SSL key validation when making requests to the registry via HTTPS.
See also the ca
option.
network-concurrency
- Default: 16
- 類型:Number
Controls the maximum number of HTTP(S) requests to process simultaneously.
fetch-retries
- Default: 2
- 類型:Number
How many times to retry if pnpm fails to fetch from the registry.
fetch-retry-factor
- Default: 10
- Type: Number
The exponential factor for retry backoff.
fetch-retry-mintimeout
- Default: 10000 (10 seconds)
- Type: Number
The minimum (base) timeout for retrying requests.
fetch-retry-maxtimeout
- Default: 60000 (1 minute)
- Type: Number
The maximum fallback timeout to ensure the retry factor does not make requests too long.
fetch-timeout
- Default: 60000 (1 minute)
- Type: Number
The maximum amount of time to wait for HTTP requests to complete.
Peer Dependency 設定
auto-install-peers
- 預設值:false
- 類型:Boolean
若為 true
,則將自動安裝任何遺失的非選用對等相依性。
dedupe-peer-dependents
Added in: v7.29.0
- 預設值:false
- 類型:Boolean
When this setting is set to true
, packages with peer dependencies will be deduplicated after peers resolution.
For instance, let's say we have a workspace with two projects and both of them have webpack
in their dependencies. webpack
has esbuild
in its optional peer dependencies, and one of the projects has esbuild
in its dependencies. In this case, pnpm will link two instances of webpack
to the node_modules/.pnpm
directory: one with esbuild
and another one without it:
node_modules
.pnpm
webpack@1.0.0_esbuild@1.0.0
webpack@1.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0/node_modules/webpack
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
This makes sense because webpack
is used in two projects, and one of the projects doesn't have esbuild
, so the two projects cannot share the same instance of webpack
. However, this is not what most developers expect, especially since in a hoisted node_modules
, there would only be one instance of webpack
. Therefore, you may now use the dedupe-peer-dependents
setting to deduplicate webpack
when it has no conflicting peer dependencies (explanation at the end). In this case, if we set dedupe-peer-dependents
to true
, both projects will use the same webpack
instance, which is the one that has esbuild
resolved:
node_modules
.pnpm
webpack@1.0.0_esbuild@1.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
What are conflicting peer dependencies? By conflicting peer dependencies we mean a scenario like the following one:
node_modules
.pnpm
webpack@1.0.0_react@16.0.0_esbuild@1.0.0
webpack@1.0.0_react@17.0.0
project1
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0/node_modules/webpack
react (v17)
project2
node_modules
webpack -> ../../node_modules/.pnpm/webpack@1.0.0_esbuild@1.0.0/node_modules/webpack
esbuild
react (v16)
In this case, we cannot dedupe webpack
as webpack
has react
in its peer dependencies and react
is resolved from two different versions in the context of the two projects.
strict-peer-dependencies
- Default: false (was true from v7.0.0 until v7.13.5)
- 類型:Boolean
If this is enabled, commands will fail if there is a missing or invalid peer dependency in the tree.
resolve-peers-from-workspace-root
Added in: v7.23.0
- 預設值:false
- 類型:Boolean
When enabled, dependencies of the root workspace project are used to resolve peer dependencies of any projects in the workspace. It is a useful feature as you can install your peer dependencies only in the root of the workspace, and you can be sure that all projects in the workspace use the same versions of the peer dependencies.