pnpm pack

从软件包中创建一个 tarball。

Files included in the tarball#

Use the files field in package.json to select files and directories. Patterns support extglobs such as lib/!(*.test.js) and @(dist|lib). A files field replaces the root .npmignore or .gitignore; ignore files inside selected subdirectories still apply. An explicitly named file or internal symlink is included even when its parent directory is excluded.

Brace ranges expand to individual values. For example, part{1..3}.js matches part1.js, part2.js, and part3.js, and part{a..c}.js matches parta.js, partb.js, and partc.js. Ranges support descending values, steps such as part{1..5..2}.js, and zero padding such as part{01..03}.js. A zero step uses the default increment of 1. Padding in a step such as part{1..10..02}.js also pads the results to the wider endpoint's width. These ranges also work in .npmignore and .gitignore.

Without files, pnpm reads .npmignore, falling back to .gitignore. Negated ignore patterns can re-include default exclusions such as *.orig and npm-debug.log. Root lockfiles and .npmrc stay excluded.

Private workspace dependencies#

A package that is not private cannot list a private workspace package in dependencies, because the private package is never published. Packing such a package fails with ERR_PNPM_PRIVATE_WORKSPACE_DEPENDENCY. List the private package in devDependencies instead, bundle it by listing it in bundleDependencies with it installed, or remove the dependency in a beforePacking hook.

配置项#

--recursive, -r#

打包工作空间(Workspace)中的所有包。

--out <path>#

自定义 tarball 的输出路径。可以使用 %s 和 %v来添加软件包名称和版本,例如, %s.tgz 或 some-dir/%s-%v.tgz。默认情况下,tarball 以名称 <package-name>-<version>.tgz 保存在当前工作目录中。

--pack-destination <dir>#

pnpm pack 保存 tarball 的目录。默认值为当前目录。

--pack-gzip-level <level>#

The gzip compression level of the tarball, from 0 to 9. Values outside that range are rejected.

--json#

JSON 格式的日志输出。 Each archive written has an integrity, its SHA-512 in SRI form, and a size in bytes. A dry run writes no archive, so it prints neither.

The manifest field holds the package.json that goes into the tarball, after publishConfig and the other publish-time changes are applied. Run pnpm pack --dry-run --json to preview it without writing a tarball. A dry run still runs prepack and prepare, unless --ignore-scripts is passed.

--filter <package_selector>#

阅读更多有关过滤的内容。

--dry-run#

做正常运行的一切,除了打包 tarball。可用于验证 tarball 包的内容。

--ignore-pnpmfile#

Don't load the project's .pnpmfile.cjs, so its hooks, including beforePacking, don't run.

--ignore-scripts#

Don't run the prepack, prepare, and postpack scripts.

--skip-manifest-obfuscation#

在打包后的清单中保留原始的 packageManager 字段和发布生命周期脚本,而不是将其剔除。 The pnpm field is still omitted, except for pnpm.config.

Life cycle scripts#

  • prepack
  • prepare
  • postpack

提示

你还可以使用 beforePacking 钩子 在创建 tarball 之前以编程方式修改 package.json 的内容。这对于删除仅用于开发的字段或添加发布元数据非常有用,而无需修改本地的 package.json。