package.json
一个包的清单文件。 它包含包的所有元数据,包括依赖项、标题、作者等等。 这是所有主要的 Node.js 包管理工具,包括 pnpm 的保留标准。
engines
你可以指定你的软件能够运行的 Node 版本和 pnpm 版本:
{
"engines": {
"node": ">=10",
"pnpm": ">=3"
}
}
在本地开发时, 如果其版本与 engines
字段中指定的版本不匹配,pnpm 将始终失败并报错。
当你的包作为依赖被安装时,除非用户已设置 engine-strict
配置标志 (参阅 .npmrc),否则此字段仅供参考,且只会产生警告。
dependenciesMeta
用于在 dependencies
, optionalDependencies
和 devDependencies
中声明的依赖项的补充元信息。
dependenciesMeta.*.injected
如果将本地工作区包依赖项设置为 true
,则将通过在虚拟存储中创建硬链接副本来安装该包(node_modules/.pnpm
)。
如果将其设置为 false
或未设置,则将通过创建指向工作空间中包的源目录的 node_modules
符号链接来安装依赖项。 这是缺省值,因为它更快,并确保对依赖关系的任何修改都将立即对其使用者可视。
例如,假设以下 package.json
是一个本地工作区包:
{
"name": "card",
"dependencies": {
"button": "workspace:1.0.0"
}
}
The button
dependency will normally be installed by creating a symlink in the node_modules
directory of card
, pointing to the development directory for button
.
But what if button
specifies react
in its peerDependencies
? If all projects in the monorepo use the same version of react
, then there is no problem. But what if button
is required by card
that uses react@16
and form
that uses react@17
? Normally you'd have to choose a single version of react
and specify it using devDependencies
of button
. Symlinking does not provide a way for the react
peer dependency to be satisfied differently by different consumers such as card
and form
.
The injected
field solves this problem by installing a hard linked copies of button
in the virtual store. To accomplish this, the package.json
of card
could be configured as follows:
{
"name": "card",
"dependencies": {
"button": "workspace:1.0.0",
"react": "16"
},
"dependenciesMeta": {
"button": {
"injected": true
}
}
}
Whereas the package.json
of form
could be configured as follows:
{
"name": "form",
"dependencies": {
"button": "workspace:1.0.0",
"react": "17"
},
"dependenciesMeta": {
"button": {
"injected": true
}
}
}
With these changes, we say that button
is an "injected dependency" of card
and form
. When button
imports react
, it will resolve to react@16
in the context of card
, but resolve to react@17
in the context of form
.
由于注入依赖关系会生成其工作空间源目录的副本,因此每当修改代码时,都必须更新这些副本; 否则,新状态将不会反映给使用者。 When building multiple projects with a command such as pnpm --recursive run build
, this update must occur after each injected package is rebuilt but before its consumers are rebuilt. For simple use cases, it can be accomplished by invoking pnpm install
again, perhaps using a package.json
lifecycle script such as "prepare": "pnpm run build"
to rebuild that one project. Third party tools such as pnpm-sync and pnpm-sync-dependencies-meta-injected provide a more robust and efficient solution for updating injected dependencies, as well as watch mode support.
peerDependenciesMeta
This field lists some extra information related to the dependencies listed in
the peerDependencies
field.
peerDependenciesMeta.*.optional
如果设置为 true,所选的 peer dependency 将被包管理工具标记为可选的。 因此,消费方省略它将不再是 被报告为错误。
示例:
{
"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 将假定任何版本的 bar 都是被允许的。
However, foo
is optional, but only to the required version specification.
publishConfig
在包被打包之前,可以覆盖清单中的某些字段。 以下字段可以被覆盖:
bin
main
Exports
types
或typings
module
browser
esnext
- [
es2015
](https://github.com/stereobooster/package.json#es2015) unpkg
umd:main
typesVersions
- cpu
- os
要覆盖字段,请将该字段的发布版本添加到 publishConfig
。
例如,以下 package.json
:
{
"name": "foo",
"version": "1.0.0",
"main": "src/index.ts",
"publishConfig": {
"main": "lib/index.js",
"typings": "lib/index.d.ts"
}
}
将被发布为:
{
"name": "foo",
"version": "1.0.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts"
}
publishConfig.executableFiles
默认情况下,出于可移植性的原因,除了 bin 字段中列出的文件之外,不会在生成的包存档中将任何文件标记为可执行文件。 The executableFiles
field lets you declare additional files 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
.
预计在指定目录中有当前包的修改版本(通常使用第三方构建工具)。
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
此字段允许您指示 pnpm 覆盖依赖关系图中的任何依赖项。 这对于强制所有软件包使用一个依赖项的单个版本、反向移植一个修复、用分叉替换依赖项或删除未使用的依赖项很有用。
请注意,overrides 字段只能在项目的根目录下设置。
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"
}
}
}