package.json

一个包的清单文件。它包含包的所有元数据,包括依赖项、标题、作者等等。这是所有主要的 Node.js 包管理工具,包括 pnpm 的保留标准。

In addition to the traditional package.json format, pnpm also supports package.json5 (in JSON5 format) and package.yaml (in YAML format).

注意

pnpm does not read settings from the pnpm field of package.json. Define settings in pnpm-workspace.yaml instead. 请参阅 配置。

bin#

The bin field maps command names to files inside the package. pnpm normalizes backslash separators to / and parent directory segments at the package root. For example, ../dist/cli.js resolves to dist/cli.js inside the package.

engines#

你可以指定你的软件能够运行的 Node 版本和 pnpm 版本:

package.json
{
    "engines": {
        "node": ">=10",
        "pnpm": ">=3"
    }
}

If the project's engines.pnpm does not accept the running pnpm version, the install fails.

With engineStrict enabled, an incompatible engines.node in the project or in a dependency fails the install. Optional dependencies are exempt. With it disabled, an incompatible engines.node in the project prints a warning. The required dependencies with an incompatible engines.node are listed at the end of the install output.

In a workspace, these checks apply to every workspace project, even when pnpm add or pnpm remove changes only one of them. pnpm run -r and pnpm exec -r run them too, before running anything. A project whose os, cpu, or libc excludes the current platform prints a warning.

A project's engines.node is checked against the first of these that applies:

  1. The nodeVersion setting, when it is set.
  2. The Node.js runtime linked for the project from dependencies or devDependencies. A production install does not include a runtime from devDependencies.
  3. The root project's runtime pin.
  4. The system Node.js.

When a lockfile records the linked runtime, pnpm checks its resolved version. Before resolution, pnpm uses the declared runtime version or the lowest version allowed by its range.

--no-runtime skips the two runtime pins.

engines.runtime#

指定依赖项所需的 Node.js 运行时环境。当声明时,pnpm 将自动安装指定的 Node.js 版本。

package.json
{
  "engines": {
    "runtime": {
      "name": "node",
      "version": "^24.11.0",
      "onFail": "download"
    }
  }
}

当一个包声明运行时时:

  1. 对于 CLI 应用程序:pnpm 将 CLI 绑定到所需的 Node.js 版本,确保无论全局安装的 Node.js 实例如何,它都能使用正确的运行时。
  2. 对于带有 postinstall 脚本的软件包:该脚本使用指定的 Node.js 版本执行。

这对于需要特定 Node.js 版本才能正常运行的依赖项尤其有用。

devEngines.runtime#

允许指定项目使用的一个或多个 JavaScript 运行时引擎。支持的运行时包括 Node.js、Deno 和 Bun。

例如,下面是如何将 node@^24.4.0 添加到你的依赖项中:

package.json
{
  "devEngines": {
    "runtime": {
      "name": "node",
      "version": "^24.4.0",
      "onFail": "download"
    }
  }
}

你也可以将多个运行时添加到同一个 package.json:

package.json
{
  "devEngines": {
    "runtime": [
      {
        "name": "node",
        "version": "^24.4.0",
        "onFail": "download"
      },
      {
        "name": "deno",
        "version": "^2.4.3",
        "onFail": "download"
      }
    ]
  }
}

工作原理:

  1. pnpm install 将你指定的范围解析为最新的匹配运行时版本。
  2. 精确的版本(和校验和)保存在锁文件(lockfile)中。
  3. 脚本使用本地运行时,确保跨环境的一致性。

要覆盖声明的 onFail 行为而不编辑清单,请使用 runtimeOnFail 设置。

Since v12.0.0-rc.2, a bare node (or deno/bun) run from inside the project also follows this pin, not just scripts. This can be turned off with the globalShims setting, or skipped for a single command with PNPM_SHIM_BYPASS=1. See project-aware global bins.

packageManager#

Names the project's package manager and its exact version:

package.json
{
  "packageManager": "pnpm@12.0.0"
}

When a command runs with a different pnpm version, pnpm switches to the pinned one, as set by pmOnFail. To allow a range of versions, use devEngines.packageManager. See Package managers for other package managers.

devEngines.packageManager#

允许通过 package.json 中的 devEngines.packageManager 指定 pnpm 版本。与 packageManager 字段不同,此字段支持版本范围。解析后的版本存储在 packageManagerDependencies 下的 pnpm-lock.yaml 中,如果仍然满足范围,则会被重用。

When the lockfile records no version yet, pnpm records the running pnpm if it satisfies the range and was published at least minimumReleaseAge ago. Otherwise pnpm records the newest version in the range that is old enough. If no version in the range is old enough, pnpm records the running pnpm.

package.json
{
  "devEngines": {
    "packageManager": {
      "name": "pnpm",
      "version": ">=12.0.0 <13.0.0",
      "onFail": "download"
    }
  }
}

注意

When pnpm 12 is declared via the legacy packageManager field and pmOnFail is not set to ignore, its resolution info is written to pnpm-lock.yaml. The lockfile then gains a leading boot document.

Since v12.0.0-rc.6, the field can also name another package manager (npm, yarn or bun), which is what pnpm add npm@11 writes. For those, nothing is recorded in pnpm-lock.yaml: packageManagerDependencies holds pnpm's own pin, and another package manager is pinned in an environment lockfile of its own under the pnpm home.

要覆盖 onFail 行为而不编辑清单,请参阅 pmOnFail 设置。

dependenciesMeta#

用于在 dependencies, optionalDependencies 和 devDependencies 中声明的依赖项的补充元信息。

dependenciesMeta.*.injected#

如果将本地工作区包依赖项设置为 true,则将通过在虚拟存储中创建硬链接副本来安装该包(node_modules/.pnpm)。

如果将其设置为 false 或未设置,则将通过创建指向工作空间中包的源目录的 node_modules 符号链接来安装依赖项。这是缺省值,因为它更快,并确保对依赖关系的任何修改都将立即对其使用者可视。

例如,假设以下 package.json 是一个本地工作区包:

package.json
{
  "name": "card",
  "dependencies": {
    "button": "workspace:1.0.0"
  }
}

button 依赖项通常会通过在 card 的 node_modules 目录中创建符号链接来安装,该符号链接指向 button 的开发目录。

但是,如果 button 在其 peerDependencies 中指定 react,该怎么办?如果 monorepo 中的所有项目都使用相同版本的 react,那么就没有问题。但如果 card 依赖的 button 使用 react@16 并且 form 使用 react@17 呢?通常你必须选择一个版本的 react 并使用 button 的 devDependencies 来指定它。符号链接无法为 react 对等依赖提供一种方式,使不同的消费者(例如 card 和 form)能够以不同的方式满足它。

injected 字段通过在虚拟存储中安装 button 的硬链接副本解决了这个问题。为了实现这一点, card 的 package.json 可以按如下方式配置:

package.json
{
  "name": "card",
  "dependencies": {
    "button": "workspace:1.0.0",
    "react": "16"
  },
  "dependenciesMeta": {
    "button": {
      "injected": true
    }
  }
}

而 form 的 package.json 可以按如下方式配置:

package.json
{
  "name": "form",
  "dependencies": {
    "button": "workspace:1.0.0",
    "react": "17"
  },
  "dependenciesMeta": {
    "button": {
      "injected": true
    }
  }
}

通过这些变化,我们说 button 是 card 和 form 的“注入依赖项”。当 button 导入react 时,它将在 card 的上下文中解析为 react@16,但在 form 的上下文中解析为 react@17。

由于注入依赖关系会生成其工作空间源目录的副本,因此每当修改代码时,都必须更新这些副本; 否则,新状态将不会反映给使用者。当使用命令(例如 pnpm --recursive run build)构建多个项目时,此更新必须在重建每个注入的包之后但重建其使用者之前进行。对于简单的用例,可以通过再次调用 pnpm install 来完成,也许是使用 package.json 生命周期脚本(如 "prepare": "pnpm run build")来重建该项目。 The syncInjectedDepsAfterScripts setting refreshes the injected copies after the listed scripts run, and while they run. Third party tools such as pnpm-sync and pnpm-sync-dependencies-meta-injected also update injected dependencies, with watch mode support.

peerDependencies#

对等依赖的值通常是语义范围(如 ^1.0.0),或者是 workspace: 或 catalog: 指定符。

A peer dependency may also be declared with a specifier that carries a scheme:

package.json
{
  "peerDependencies": {
    "lib-a": "work:5.x.x",
    "lib-b": "npm:other-lib@^5",
    "lib-c": "file:../lib-c",
    "lib-d": "git+https://example.com/lib-d.git"
  }
}

支持的格式包括 命名注册源 指定符(<registry>:<version>)、npm: 别名,以及 file:、git 或 URL 指定符。

Such a specifier is matched against the semver range it carries: work:5.x.x is checked as 5.x.x and npm:other-lib@^5 as ^5. 不包含版本信息的指定符(例如 file:../lib-c)会与 * 匹配,因此任何版本均满足该条件。与此同时,当 autoInstallPeers 安装缺失的对等依赖时,正是最初指定的标识符决定了要安装哪个包;因此,该对等依赖会根据你指定的别名、注册源或来源来进行获取。

仅包含 name@version 的值仍会被 ERR_PNPM_INVALID_PEER_DEPENDENCY_SPECIFICATION 错误拒绝,因为这几乎总是错误的:

package.json
{
  "peerDependencies": {
    "lib-a": "lib-a@1.2.3"
  }
}

peerDependenciesMeta#

此字段列出了一些与 peerDependencies 字段中列出的依赖关系相关的额外信息。

peerDependenciesMeta.*.optional#

如果设置为 true,所选的 peer dependency 将被包管理工具标记为可选的。因此,消费方省略它将不再被报告为错误。

例如:

package.json
{
    "peerDependencies": {
        "foo": "1"
    },
    "peerDependenciesMeta": {
        "foo": {
            "optional": true
        },
        "bar": {
            "optional": true
        }
    }
}

Even though bar was not specified in peerDependencies, it is marked as optional. 因此,pnpm 将假定任何版本的 bar 都是被允许的。 A missing foo causes no warning either, but an installed foo whose version does not satisfy 1 still produces a peer dependency warning.

publishConfig#

This field carries settings that change how a package is published: registry, access, directory, linkDirectory, executableFiles, and @<scope>:registry. These are preserved in the published package.json.

Additionally, publishConfig can be used to override several other fields in the manifest before the package is packed. 以下字段可以被覆盖:

要覆盖字段,请将该字段的发布版本添加到 publishConfig。

例如,以下 package.json:

package.json
{
    "name": "foo",
    "version": "1.0.0",
    "main": "src/index.ts",
    "publishConfig": {
        "main": "lib/index.js",
        "typings": "lib/index.d.ts"
    }
}

将被发布为:

package.json
{
    "name": "foo",
    "version": "1.0.0",
    "main": "lib/index.js",
    "typings": "lib/index.d.ts"
}

publishConfig.registry#

The registry this package publishes to. It overrides the registry setting, any scope route in registries, and pnpm publish --registry.

package.json
{
  "name": "foo",
  "version": "1.0.0",
  "publishConfig": {
    "registry": "https://registry.npmjs.org/"
  }
}

Only the URL belongs here. pnpm matches it against the auth settings in .npmrc, so the example above publishes with //registry.npmjs.org/:_authToken=....

Each package in a pnpm publish -r run goes to its own target registry, so one command can release a workspace to several registries. --batch then sends one request per registry.

Since v12.7.0, a scoped package can also set its registry under the @<scope>:registry key. For a package in that scope, it takes precedence over publishConfig.registry and over the registry set for the scope in .npmrc:

package.json
{
  "name": "@acme/foo",
  "version": "1.0.0",
  "publishConfig": {
    "@acme:registry": "https://npm.acme.example/"
  }
}

publishConfig.access#

Tells the registry whether the published package should be public or restricted. Same as pnpm publish --access.

Left unset, pnpm sends no access level, so the registry applies its own default and the level of an already published package is left alone. Unscoped packages cannot be restricted.

package.json
{
  "name": "@acme/foo",
  "version": "1.0.0",
  "publishConfig": {
    "access": "public"
  }
}

publishConfig.name#

以不同于其清单在工作区中所用名称的另一个名称发布该包。适用于发布名称已被同级项目占用的项目,否则就只能在发布前的构建步骤中临时改名。

package.json
{
  "name": "foo-v2",
  "version": "2.0.0",
  "publishConfig": {
    "name": "foo"
  }
}

In a packed dependent, a shorthand such as workspace:^ or a relative path such as workspace:../foo-v2 uses the dependency's published name. Dependents, pnpm-lock.yaml, and release tooling keep addressing the project by its manifest name. 新名称会体现在打包后的清单、tarball 文件名,以及注册源中所有引用该包的地方:pnpm publish -r 的已发布检查、其注册源选择,以及 pnpm change status 与 pnpm version -r 的发布规划探测。

publishConfig.executableFiles#

默认情况下,出于可移植性的原因,除了 bin 字段中列出的文件之外,不会在生成的包存档中将任何文件标记为可执行文件。 executableFiles 字段允许您声明必须设置可执行标志 (+x) 的额外字段,即使它们不能通过 bin 字段直接访问。

package.json
{
  "publishConfig": {
    "executableFiles": [
      "./dist/shim.js"
    ]
  }
}

publishConfig.directory#

你还可以使用 publishConfig.directory 字段来自定义相对于当前 package.json 的发布子目录。

预计在指定目录中有当前包的修改版本(通常使用第三方构建工具)。

在这个例子中 "dist" 文件夹必须包含一个 package.json

package.json
{
  "name": "foo",
  "version": "1.0.0",
  "publishConfig": {
    "directory": "dist"
  }
}

publishConfig.linkDirectory#

  • 默认值:true
  • 类型:Boolean

当设置为 true 时,项目将在本地开发期间从 publishConfig.directory 位置进行符号链接。

例如:

package.json
{
  "name": "foo",
  "version": "1.0.0",
  "publishConfig": {
    "directory": "dist",
    "linkDirectory": true
  }
}