package.json
package.json 是套件的資訊清單檔。 它包含套件的所有中繼資料,包括相依性、標題、作者等。 這是適用於所有主流 Node.JS 的一種標準套件管理器 (包含 pnpm)。
engines
您可以指定軟體執行的 Node 與 pnpm 版本:
{
"engines": {
"node": ">=10",
"pnpm": ">=3"
}
}
在本機開發期間,如果使用的版本與 engines
區塊中指定的版本不相符,pnpm 一定會失敗並顯示錯誤訊息。
除非使用者設定了 engine-strict
的設定旗標 (請參閱 .npmrc),否則 此區塊僅供參考,並且只有在套件進行相依性安裝時產生警告訊息。
dependenciesMeta
附加的中繼資料,用於 dependencies
、optionalDependencies
及 devDependencies
中宣告的依附項目 (dependency)。
dependenciesMeta.*.injected
If this is set to true
for a local dependency, the package will be hard linked to the virtual store (node_modules/.pnpm
) and symlinked from the virtual store to the modules directory.
If this is set to false
or not set for a local dependency, the package will be symlinked directly from its location in the workspace to the module directory.
For instance, the following package.json
in a workspace will create a symlink to button
in the node_modules
directory of card
:
{
"name": "card",
"dependencies": {
"button": "workspace:1.0.0"
}
}
But what if button
has react
in its peer dependencies? If all projects in the monorepo use the same version of react
, then no problem. But what if button
is required by card
that uses react@16
and form
with react@17
? Without using inject
, you'd have to choose a single version of react
and install it as dev dependency of button
. But using the injected
field you can inject button
to a package, and button
will be installed with the react
version of that package.
So this will be the package.json
of card
:
{
"name": "card",
"dependencies": {
"button": "workspace:1.0.0",
"react": "16"
},
"dependenciesMeta": {
"button": {
"injected": true
}
}
}
button
will be hard linked into the dependencies of card
, and react@16
will be symlinked to the dependencies of card/node_modules/button
.
And this will be the package.json
of form
:
{
"name": "form",
"dependencies": {
"button": "workspace:1.0.0",
"react": "17"
},
"dependenciesMeta": {
"button": {
"injected": true
}
}
}
button
will be hard linked into the dependencies of form
, and react@17
will be symlinked to the dependencies of form/node_modules/button
.
In contrast to normal dependencies, injected ones are not symlinked to the destination folder, so they are not updated automatically, e.g. after running the build script. To update the hard linked folder contents to the latest state of the dependency package folder, call pnpm i
again.
Note that the button
package must have any lifecycle script that runs on install in order for pnpm
to detect the changes and update it. For example, the package can be rebuilt on install: "prepare": "pnpm run build"
. Any script would work, even a simple unrelated command without side effects, like this: "prepare": "pnpm root"
.
peerDependenciesMeta
This field lists some extra information related to the dependencies listed in the peerDependencies
field.
peerDependenciesMeta.*.optional
If this is set to true, the selected peer dependency will be marked as optional by the package manager. Therefore, the consumer omitting it will no longer be reported as an error.
範例:
{
"peerDependencies": {
"foo": "1"
},
"peerDependenciesMeta": {
"foo": {
"optional": true
},
"bar": {
"optional": true
}
}
}
Note that even though bar
was not specified in peerDependencies
, it is marked as optional. pnpm will therefore assume that any version of bar is fine. However, foo
is optional, but only to the required version specification.
publishConfig
It is possible to override some fields in the manifest before the package is packed. The following fields may be overridden:
To override a field, add the publish version of the field to publishConfig
.
For instance, the following package.json
:
{
"name": "foo",
"version": "1.0.0",
"main": "src/index.ts",
"publishConfig": {
"main": "lib/index.js",
"typings": "lib/index.d.ts"
}
}
Will be published as:
{
"name": "foo",
"version": "1.0.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts"
}
publishConfig.executableFiles
By default, for portability reasons, no files except those listed in the bin field will be marked as executable in the resulting package archive. The executableFiles
field lets you declare additional fields that must have the executable flag (+x) set even if they aren't directly accessible through the bin field.
{
"publishConfig": {
"executableFiles": [
"./dist/shim.js"
]
}
}
publishConfig.directory
You also can use the field publishConfig.directory
to customize the published subdirectory relative to the current package.json
.
It is expected to have a modified version of the current package in the specified directory (usually using third party build tools).
In this example the
"dist"
folder must contain apackage.json
{
"name": "foo",
"version": "1.0.0",
"publishConfig": {
"directory": "dist"
}
}
publishConfig.linkDirectory
- 預設值:true
- 類型:Boolean
When set to true
, the project will be symlinked from the publishConfig.directory
location during local development.
範例:
{
"name": "foo",
"version": "1.0.0",
"publishConfig": {
"directory": "dist"
"linkDirectory": true
}
}
pnpm.overrides
This field allows you to instruct pnpm to override any dependency in the dependency graph. This is useful to enforce all your packages to use a single version of a dependency, backport a fix, or replace a dependency with a fork.
Note that the overrides field can only be set at the root of the project.
An example of the "pnpm"."overrides"
field:
{
"pnpm": {
"overrides": {
"foo": "^1.0.0",
"quux": "npm:@myorg/quux@^1.0.0",
"bar@^2.1.0": "3.0.0",
"qar@1>zoo": "2"
}
}
}