Settings (.npmrc)
pnpm gets its configuration from the command line, environment variables, and
.npmrc
files.
The pnpm config
command can be used to update and edit the contents of the
user and global .npmrc
files.
四個相關的檔案是:
- per-project configuration file (
/path/to/my/project/.npmrc
) - per-workspace configuration file (the directory that contains the
pnpm-workspace.yaml
file) - per-user configuration file (
~/.npmrc
) - global configuration file (
/etc/npmrc
)
All .npmrc
files are an INI-formatted list of key = value
parameters.
Values in the .npmrc
files may contain env variables using the ${NAME}
syntax. 您也可以使用預設值來指定環境變數。 Using ${NAME-fallback}
will return fallback
if NAME
isn't set. ${NAME:-fallback}
will return fallback
if NAME
isn't set, or is an empty string.
相依性提升設定
hoist
- Default: true
- Type: boolean
When true
, all dependencies are hoisted to node_modules/.pnpm/node_modules
. This makes
unlisted dependencies accessible to all packages inside node_modules
.
hoist-workspace-packages
- Default: true
- Type: boolean
When true
, packages from the workspaces are symlinked to either <workspace_root>/node_modules/.pnpm/node_modules
or to <workspace_root>/node_modules
depending on other hoisting settings (hoist-pattern
and public-hoist-pattern
).
hoist-pattern
- Default: ['*']
- Type: string[]
Tells pnpm which packages should be hoisted to node_modules/.pnpm/node_modules
. 預設情況下,所有套件都會提升,但是如果你知道只有一些有缺陷套件有幻影依賴關係, 您可以使用此選項來只提升有幻影依賴關係的套件 (推薦)。
例如:
hoist-pattern[]=*eslint*
hoist-pattern[]=*babel*
You may also exclude patterns from hoisting using !
.
例如:
hoist-pattern[]=*types*
hoist-pattern[]=!@types/react
public-hoist-pattern
- Default: ['*eslint*', '*prettier*']
- Type: string[]
Unlike hoist-pattern
, which hoists dependencies to a hidden modules directory
inside the virtual store, public-hoist-pattern
hoists dependencies matching
the pattern to the root modules directory. 提升至根模組目錄表示應用程式程式碼將可以存取虛設的相依性,即使這個程式碼對解決方案策略進行了不當的修改也一樣。
在處理一些不當解析相依性的錯誤可插入式工具時,這個設定很有用。
例如:
public-hoist-pattern[]=*plugin*
Note: Setting shamefully-hoist
to true
is the same as setting
public-hoist-pattern
to *
.
You may also exclude patterns from hoisting using !
.
例如:
public-hoist-pattern[]=*types*
public-hoist-pattern[]=!@types/react
shamefully-hoist
- Default: false
- Type: Boolean
By default, pnpm creates a semistrict node_modules
, meaning dependencies have
access to undeclared dependencies but modules outside of node_modules
do not.
使用此頁面配置,生態系統中的大多數套件將沒有問題的運作。
However, if some tooling only works when the hoisted dependencies are in the
root of node_modules
, you can set this to true
to hoist them for you.
節點模組設定
modules-dir
- Default: node_modules
- Type: path
The directory in which dependencies will be installed (instead of
node_modules
).
node-linker
- Default: isolated
- Type: isolated, hoisted, pnp
定義應該使用哪些連結器來安裝 Node 套件。
- isolated - dependencies are symlinked from a virtual store at
node_modules/.pnpm
. - hoisted - a flat
node_modules
without symlinks is created. Same as thenode_modules
created by npm or Yarn Classic. 使用這個設定時,有一個 Yarn 程式庫用於提升。 使用此設定的合法原因:- 您的工具無法與符號連結一起使用。 A React Native project will most probably only work if you use a hoisted
node_modules
. - 您的專案部署在一個無伺服器的主機服務提供者。 一些無伺服器的提供者 (例如 AWS Lambia) 不支援符號連結。 或者,您也可以在部署之前組合應用程式來解決此問題。
- If you want to publish your package with
"bundledDependencies"
. - If you are running Node.js with the --preserve-symlinks flag.
- 您的工具無法與符號連結一起使用。 A React Native project will most probably only work if you use a hoisted
- pnp - no
node_modules
. Plug'n'Play is an innovative strategy for Node that is used by Yarn Berry. It is recommended to also setsymlink
setting tofalse
when usingpnp
as your linker.
symlink
- Default: true
- Type: Boolean
When symlink
is set to false
, pnpm creates a virtual store directory without
any symlinks. It is a useful setting together with node-linker=pnp
.
enable-modules-dir
- Default: true
- Type: Boolean
When false
, pnpm will not write any files to the modules directory
(node_modules
). 這對於模組目錄同使用者空間內檔案系統 (FUSE) 裝載時有幫助。 There is an experimental CLI that allows you to
mount a modules directory with FUSE: @pnpm/mount-modules.
virtual-store-dir
- Default: node_modules/.pnpm
- Types: path
包含指向存放區連結的目錄。 專案所有直接和間接依賴的依賴套件都建立了指向此目錄的連結。
這個設定對解決 Windows 上長路徑的問題有幫助。 If
you have some dependencies with very long paths, you can select a virtual store
in the root of your drive (for instance C:\my-project-store
).
Or you can set the virtual store to .pnpm
and add it to .gitignore
. 這將會讓 StrackTrace 更加簡潔,因為相依套件的路徑將會高出一個目錄。
NOTE: the virtual store cannot be shared between several projects. 每個專案都應該有自己的虛擬存放區 (共用根的工作區內除外)。
virtual-store-dir-max-length
Added in: v9.1.0
- Default: 120
- Types: number
Sets the maximum allowed length of directory names inside the virtual store directory (node_modules/.pnpm
). You may set this to a lower number if you encounter long path issues on Windows.
package-import-method
- Default: auto
- Type: 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 - try to clone packages from the store. 如果不支援複製,則從存放區建立指向套的 Hardlink。 如果再製和建立連結都不可行,則回到複製
- hardlink - hard link packages from the store
- clone-or-copy - try to clone packages from the store. 如果不支援再製,則回到複製
- copy - copy packages from the store
- clone - clone (AKA copy-on-write or reference link) packages from the store
再製是將套件寫入 node_modules 的最佳方式。 這是最快速、最安全的方式。 使用再製時,您可以在 node_modules 中編輯檔案,而在中央的內容可定址存放區中,它們將不會被修改。
不過,不是所有的檔案系統都支援再製。 我們推薦使用寫入時複製 (CoW) 檔案系統 (例如,在 Linux 上使用 Btrfs 而非 Ext4),以取得使用 pnpm 的最佳體驗。
modules-cache-max-age
- Default: 10080 (7 days in minutes)
- Type: number
模組目錄中的孤立套件應該被移除的時間 (單位為分鐘)。 pnpm 會在模組目錄中保留套件的快取。 這會提高切換分支或降級相依性時的安裝速度。
dlx-cache-max-age
- Default: 1440 (1 day in minutes)
- Type: number
The time in minutes after which dlx cache expires. After executing a dlx command, pnpm keeps a cache that omits the installation step for subsequent calls to the same dlx command.
Store Settings
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
- Type: path
所有套件儲存在磁碟上的位置。
存放區應該一律安裝在相同的磁碟上,因此每個磁碟都將有一個存放區。 如果目前的磁碟上有主目錄,則將在其內部建立儲存區。 如果磁碟上沒有主目錄,則將在檔案系統的根目錄中建立儲存區。 For
example, if installation is happening on a filesystem mounted at /mnt
,
then the store will be created at /mnt/.pnpm-store
. Windows 系統也是這樣。
從不同的磁碟設定存放區是可行的,但在那種情況下,pnpm 將不會建立指向套件的永久連結,而是會從存放區複製,因為只有在相同的檔案系統上才可以建立永久連結。
verify-store-integrity
- Default: true
- Type: Boolean
By default, if a file in the store has been modified, the content of this file is checked before linking it to a project's node_modules
. If verify-store-integrity
is set to false
, files in the content-addressable store will not be checked during installation.
use-running-store-server
Deprecated feature
- Default: false
- Type: Boolean
Only allows installation with a store server. If no store server is running, installation will fail.
strict-store-pkg-content-check
Added in: v9.4.0
- Default: true
- Type: Boolean
Some registries allow the exact same content to be published under different package names and/or versions. This breaks the validity checks of packages in the store. To avoid errors when verifying the names and versions of such packages in the store, you may set the strict-store-pkg-content-check
setting to false
.
鎖定檔設定
lockfile
- Default: true
- Type: Boolean
When set to false
, pnpm won't read or generate a pnpm-lock.yaml
file.
prefer-frozen-lockfile
- Default: true
- Type: Boolean
When set to true
and the available pnpm-lock.yaml
satisfies the
package.json
dependencies directive, a headless installation is performed. 無周邊安裝會略過所有的相依性解析,因為它不需要修改鎖定檔。
lockfile-include-tarball-url
- Default: false
- Type: Boolean
Add the full URL to the package's tarball to every entry in pnpm-lock.yaml
.
git-branch-lockfile
- Default: false
- Type: 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
- 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 !
.
peers-suffix-max-length
Added in: v9.3.0
- Default: 1000
- Type: number
Max length of the peer IDs suffix added to dependency keys in the lockfile. If the suffix is longer, it is replaced with a hash.
Registry & Authentication Settings
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
- Type: 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.
<URL>:cafile
Define the path to a Certificate Authority file to use when accessing the specified registry. 範例:
//registry.npmjs.org/:keyfile=client-cert.pem