跳至主要內容
版本:7.x

.npmrc

pnpm 從命令命令列、環境變數和 .npmrc 檔中取得組態。

pnpm config 命令可以用於更新或編輯使用者及全域 .npmrc 檔的內容。

四個相關的檔案是:

  • 每個專案的設定檔 (/path/to/my/project/.npmrc)
  • 每個工作區的設定檔 (包含 pnpm-workspace.yaml 檔的目錄)
  • 每位使用者的設定檔 (~/.npmrc)
  • 全域設定檔 (/etc/npmrc)

所有 .npmrc 檔都是 key = value 參數的 INI 格式清單。

相依性提升設定

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[]

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. Hoisting to the root modules directory means that application code will have access to phantom dependencies, even if they modify the resolution strategy improperly.

This setting is useful when dealing with some flawed pluggable tools that don't resolve dependencies properly.

例如:

public-hoist-pattern[]=*plugin*

Note: Setting shamefully-hoist to true is the same as setting public-hoist-pattern to *.

自 v7.12.0 起,您可能還需要使用 ! 來從提升中排除模式。

例如:

public-hoist-pattern[]=*types*
public-hoist-pattern[]=!@types/react

shamefully-hoist

  • 預設值:false
  • 類型:Boolean

By default, pnpm creates a semistrict node_modules, meaning dependencies have access to undeclared dependencies but modules outside of node_modules do not. With this layout, most of the packages in the ecosystem work with no issues. 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.

節點模組設定

store-dir

  • 預設值:
    • 如果已設定環境變數 $XDG_DATA_HOME,則為 $XDG_DATA_HOME/pnpm/store
    • Windows: ~/AppData/Local/pnpm/store
    • MacOS: ~/Library/pnpm/store
    • Linux: ~/.local/share/pnpm/store
  • 類型:path

The location where all the packages are saved on the disk.

The store should be always on the same disk on which installation is happening, so there will be one store per disk. 如果目前的磁碟上有主目錄,則將在其內部建立儲存區。 如果磁碟上沒有主目錄,則將在檔案系統的根目錄中建立儲存區。 For example, if installation is happening on a filesystem mounted at /mnt, then the store will be created at /mnt/.pnpm-store. The same goes for Windows systems.

It is possible to set a store from a different disk but in that case pnpm will copy packages from the store instead of hard-linking them, as hard links are only possible on the same filesystem.

modules-dir

  • 預設值:node_modules
  • 類型:path

The directory in which dependencies will be installed (instead of node_modules).

node-linker

  • 預設值:isolated
  • 類型:isolated, hoisted, pnp

Defines what linker should be used for installing Node packages.

  • isolated - 從位於 node_modules/.pnpm 的虛擬存放區建立指向相依性的符號連結。
  • hoisted - 沒有建立符號連結的一般 node_modules。 與 npm 或 Yarn 傳統版建立的 node_modules 相同。 One of Yarn's libraries is used for hoisting, when this setting is used. 使用此設定的合法原因:
    1. 您的工具無法與符號連結一起使用。 React Native 專案將很有可能僅在您使用已提升的 node_modules 時才可以使用。
    2. 您的專案部署在一個無伺服器的主機服務提供者。 一些無伺服器的提供者 (例如 AWS Lambia) 不支援符號連結。 或者,您也可以在部署之前組合應用程式來解決此問題。
    3. 您想要使用 "bundledDependencies" 發布套件。
    4. 您正在使用 --preserve-symlinks 旗標執行 Node.js。
  • pnp - 無 node_modules。 隨插即用是一種創新策略,適用於 Yarn Berry 使用的 Node。 推薦在使用 pnp 作為連結器時,也將 symlink 設定為 false
  • 預設值: true
  • 類型: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

  • 預設值: true
  • 類型:Boolean

When false, pnpm will not write any files to the modules directory (node_modules). This is useful for when the modules directory is mounted with filesystem in userspace (FUSE). There is an experimental CLI that allows you to mount a modules directory with FUSE: @pnpm/mount-modules.

virtual-store-dir

  • 預設值:node_modules/.pnpm
  • 類型:path

The directory with links to the store. All direct and indirect dependencies of the project are linked into this directory.

This is a useful setting that can solve issues with long paths on 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. This will make stacktraces cleaner as paths to dependencies will be one directory higher.

NOTE: the virtual store cannot be shared between several projects. Every project should have its own virtual store (except for in workspaces where the root is shared).

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 - 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 的最佳體驗。

資訊

即使 macOS 支援再製,但目前還是在 Node.js 中存在一個 Bug,會阻止我們在 pnpm 中使用再製。 如果您知道如何修正這個 Bug,請協助我們

modules-cache-max-age

  • 預設值: 10080 (7 天,以分鐘為單位)
  • 類型:Number

The time in minutes after which orphan packages from the modules directory should be removed. pnpm keeps a cache of packages in the modules directory. This boosts installation speed when switching branches or downgrading dependencies.

鎖定檔設定

lockfile

  • 預設值: true
  • 類型:Boolean

When set to false, pnpm won't read or generate a pnpm-lock.yaml file.

prefer-frozen-lockfile

  • 預設值: true
  • 類型:Boolean

When set to true and the available pnpm-lock.yaml satisfies the package.json dependencies directive, a headless installation is performed. A headless installation skips all dependency resolution as it does not need to modify the lockfile.

lockfile-include-tarball-url

新增於:v7.6.0

  • 預設值:false
  • 類型:Boolean

將指向套件 tarball 的完整 URL 新增至 pnpm-lock.yaml 中的每個項目。

登錄與驗證設定

registry

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. For example:

//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 

You may also use an environment variable. For example:

//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)"). For example:

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

  • 預設值: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

  • 預設值: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)"). For example:

cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"

It is not the path to a certificate file (and there is no certfile option).

key

  • 預設值: null
  • 類型: String

A client key to pass when accessing the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). For example:

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

  • 預設值: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.

local-address

  • Default: undefined
  • Type: IP Address

The IP address of the local interface to use when making connections to the npm registry.

proxy

  • 預設值: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.

maxsockets

  • Default: network-concurrency x 3
  • Type: Number

The maximum number of connections to use per origin (protocol/host/port combination).

noproxy

  • 預設值: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
  • Type: 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
  • 類型:Number

The exponential factor for retry backoff.

fetch-retry-mintimeout

  • Default: 10000 (10 seconds)
  • 類型:Number

The minimum (base) timeout for retrying requests.

fetch-retry-maxtimeout

  • Default: 60000 (1 minute)
  • 類型:Number

The maximum fallback timeout to ensure the retry factor does not make requests too long.

fetch-timeout

  • 預設值:60000 (1 分鐘)
  • Type: Number

The maximum amount of time to wait for HTTP requests to complete.

Peer Dependency 設定

auto-install-peers

  • 預設值:false
  • 類型:Boolean

若為 true,則將自動安裝任何遺失的非選用對等相依性。

strict-peer-dependencies

  • 預設值: false (在 v7.0.0 到 v7.13.5 曾是 true
  • 類型:Boolean

If this is enabled, commands will fail if there is a missing or invalid peer dependency in the tree.

CLI 設定

[no-]color

  • Default: auto
  • Type: auto, always, never

Controls colors in the output.

  • auto - output uses colors when the standard output is a terminal or TTY.
  • always - ignore the difference between terminals and pipes. You’ll rarely want this; in most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the pnpm command to force it to use color codes. The default setting is almost always what you’ll want.
  • never - turns off colors. This is the setting used by --no-color.

loglevel

  • Default: info
  • Type: debug, info, warn, error

Any logs at or higher than the given level will be shown. You can instead pass --silent to turn off all output logs.

use-beta-cli

  • 預設值:false
  • 類型:Boolean

Experimental option that enables beta features of the CLI. This means that you may get some changes to the CLI functionality that are breaking changes, or potentially bugs.

recursive-install

  • 預設值: true
  • 類型:Boolean

If this is enabled, the primary behaviour of pnpm install becomes that of pnpm install -r, meaning the install is performed on all workspace or subdirectory packages.

Else, pnpm install will exclusively build the package in the current directory.

engine-strict

  • 預設值:false
  • 類型:Boolean

If this is enabled, pnpm will not install any package that claims to not be compatible with the current Node version.

Regardless of this configuration, installation will always fail if a project (not a dependency) specifies an incompatible version in its engines field.

npm-path

  • 類型:path

The location of the npm binary that pnpm uses for some actions, like publishing.

構建設置

ignore-scripts

  • 預設值:false
  • 類型:Boolean

Do not execute any scripts defined in the project package.json and its dependencies.

注意事項

This flag does not prevent the execution of .pnpmfile.cjs

ignore-dep-scripts

新增於 v7.9.0

  • 預設值:false
  • 類型:Boolean

Do not execute any scripts of the installed packages. Scripts of the projects are executed.

child-concurrency

  • Default: 5
  • Type: Number

The maximum number of child processes to allocate simultaneously to build node_modules.

side-effects-cache

  • 預設值: true
  • 類型:Boolean

Use and cache the results of (pre/post)install hooks.

side-effects-cache-readonly

  • 預設值:false
  • 類型:Boolean

Only use the side effects cache if present, do not create it for new packages.

unsafe-perm

  • Default: false IF running as root, ELSE true
  • 類型:Boolean

Set to true to enable UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.

Node.js 設定

use-node-version

  • Default: undefined
  • 類型:semver

Specifies which exact Node.js version should be used for the project's runtime. pnpm will automatically install the specified version of Node.js and use it for running pnpm run commands or the pnpm node command.

This may be used instead of .nvmrc and nvm. Instead of the following .nvmrc file:

16.16.0

Use this .npmrc file:

use-node-version=16.16.0

node-version

  • Default: the value returned by node -v, without the v prefix
  • 類型:semver

The Node.js version to use when checking a package's engines setting.

If you want to prevent contributors of your project from adding new incompatible dependencies, use node-version and engine-strict in a .npmrc file at the root of the project:

node-version=12.22.0
engine-strict=true

This way, even if someone is using Node.js v16, they will not be able to install a new dependency that doesn't support Node.js v12.22.0.

node-mirror:<releaseDir>

  • Default: https://nodejs.org/download/<releaseDir>/
  • Type: URL

Sets the base URL for downloading Node.js. The <releaseDir> portion of this setting can be any directory from https://nodejs.org/download: release, rc, nightly, v8-canary, etc.

Here is how pnpm may be configured to download Node.js from Node.js mirror in China:

node-mirror:release=https://npmmirror.com/mirrors/node/
node-mirror:rc=https://npmmirror.com/mirrors/node-rc/
node-mirror:nightly=https://npmmirror.com/mirrors/node-nightly/

工作區設定

  • 預設值: true
  • Type: true, false, deep

If this is enabled, locally available packages are linked to node_modules instead of being downloaded from the registry. This is very convenient in a monorepo. If you need local packages to also be linked to subdependencies, you can use the deep setting.

Else, packages are downloaded and installed from the registry. However, workspace packages can still be linked by using the workspace: range protocol.

prefer-workspace-packages

  • 預設值:false
  • 類型:Boolean

If this is enabled, local packages from the workspace are preferred over packages from the registry, even if there is a newer version of the package in the registry.

This setting is only useful if the workspace doesn't use save-workspace-protocol.

shared-workspace-lockfile

  • 預設值: true
  • 類型:Boolean

If this is enabled, pnpm creates a single pnpm-lock.yaml file in the root of the workspace. This also means that all dependencies of workspace packages will be in a single node_modules (and get symlinked to their package node_modules folder for Node's module resolution).

Advantages of this option:

  • every dependency is a singleton
  • faster installations in a monorepo
  • fewer changes in code reviews as they are all in one file
注意事項

Even though all the dependencies will be hard linked into the root node_modules, packages will have access only to those dependencies that are declared in their package.json, so pnpm's strictness is preserved. This is a result of the aforementioned symbolic linking.

save-workspace-protocol

  • 預設值: true
  • 類型:true, false, rolling

This setting controls how dependencies that are linked from the workspace are added to package.json.

If foo@1.0.0 is in the workspace and you run pnpm add foo in another project of the workspace, below is how foo will be added to the dependencies field. The save-prefix setting also influences how the spec is created.

save-workspace-protocolsave-prefixspec
false''1.0.0
false'~'~1.0.0
false'^'^1.0.0
true''workspace:1.0.0
true'~'workspace:~1.0.0
true'^'workspace:^1.0.0
rolling''workspace:*
rolling'~'workspace:~
rolling'^'workspace:^

include-workspace-root

新增於 v7.4.0

  • 預設值:false
  • 類型:Boolean

When executing commands recursively in a workspace, execute them on the root workspace project as well.

其他設定

use-running-store-server

  • 預設值:false
  • 類型:Boolean

Only allows installation with a store server. If no store server is running, installation will fail.

save-prefix

  • Default: '^'
  • Type: String

Configure how versions of packages installed to a package.json file get prefixed.

For example, if a package has version 1.2.3, by default its version is set to ^1.2.3 which allows minor upgrades for that package, but after pnpm config set save-prefix='~' it would be set to ~1.2.3 which only allows patch upgrades.

This setting is ignored when the added package has a range specified. For instance, pnpm add foo@2 will set the version of foo in package.json to 2, regardless of the value of save-prefix.

tag

  • Default: latest
  • 類型:String

If you pnpm add a package and you don't provide a specific version, then it will install the package at the version registered under the tag from this setting.

This also sets the tag that is added to the package@version specified by the pnpm tag command if no explicit tag is given.

global-dir

  • 預設值:
    • If the $XDG_DATA_HOME env variable is set, then $XDG_DATA_HOME/pnpm/global
    • On Windows: ~/AppData/Local/pnpm/global
    • On macOS: ~/Library/pnpm/global
    • On Linux: ~/.local/share/pnpm/global
  • 類型:path

Specify a custom directory to store global packages.

global-bin-dir

  • 預設值:
    • If the $XDG_DATA_HOME env variable is set, then $XDG_DATA_HOME/pnpm
    • On Windows: ~/AppData/Local/pnpm
    • On macOS: ~/Library/pnpm
    • On Linux: ~/.local/share/pnpm
  • 類型:path

Allows to set the target directory for the bin files of globally installed packages.

state-dir

  • 預設值:
    • If the $XDG_STATE_HOME env variable is set, then $XDG_STATE_HOME/pnpm
    • On Windows: ~/AppData/Local/pnpm-state
    • On macOS: ~/.pnpm-state
    • On Linux: ~/.local/state/pnpm
  • 類型:path

The directory where pnpm creates the pnpm-state.json file that is currently used only by the update checker.

cache-dir

  • 預設值:
    • If the $XDG_CACHE_HOME env variable is set, then $XDG_CACHE_HOME/pnpm
    • On Windows: ~/AppData/Local/pnpm-cache
    • On macOS: ~/Library/Caches/pnpm
    • On Linux: ~/.cache/pnpm
  • 類型:path

The location of the package metadata cache.

use-stderr

  • 預設值:false
  • 類型:Boolean

When true, all the output is written to stderr.

update-notifier

  • 預設值: true
  • 類型:Boolean

Set to false to suppress the update notification when using an older version of pnpm than the latest.

prefer-symlinked-executables

新增於:v7.6.0

  • Default: true, when node-linker is set to hoisted and the system is POSIX
  • 類型:Boolean

Create symlinks to executables in node_modules/.bin instead of command shims. This setting is ignored on Windows, where only command shims work.

verify-store-integrity

新增於 v7.7.0

  • 預設值: true
  • 類型: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.

ignore-compatibility-db

新增於 v7.9.0

  • 預設值:false
  • 類型:Boolean

During installation the dependencies of some packages are automatically patched. If you want to disable this, set this config to false.

The patches are applied from Yarn's @yarnpkg/extensions package.

resolution-mode

新增於 v7.10.0

  • 預設值: highest
  • 類型: highest, time-based

When resolution-mode is set to time-based, dependencies will be resolved the following way:

  1. Direct dependencies will be resolved to their lowest versions. So if there is foo@^1.1.0 in the dependencies, then 1.1.0 will be installed.
  2. Subdependencies will be resolved from versions that were published before the last direct dependency was published.

With this resolution mode installations with warm cache are faster. It also reduces the chance of subdependency hijacking as subdependencies will be updated only if direct dependencies are updated.

This resolution mode works only with npm's full metadata. So it is slower in some scenarios. However, if you use Verdaccio v5.15.1 or newer, you may set the registry-supports-time-field setting to true, and it will be really fast.

registry-supports-time-field

新增於 v7.10.0

  • 預設值:false
  • 類型:Boolean

Set this to true if the registry that you are using returns the "time" field in the abbreviated metadata. As of now, only Verdaccio from v5.15.1 supports this.