pnpm deploy
从工作空间部署软件包。在部署期间,已部署软件包的文件将被复制到目标目录。已部署包的所有依赖项,包括来自工作区的依赖项,都安装在目标目录的独立 node_modules 目录中。目标目录将包含一个可移植包,可以无需其他步骤将其复制到服务器并执行。
When the selected project declares a link: dependency on a directory outside the workspace projects, pnpm resolves its dependencies and includes the package in the deployment.
注意
Since v12.2.0, pnpm deploy no longer requires injectWorkspacePackages. A linked workspace dependency is rewritten to a file: dependency in the dedicated deploy lockfile. The peer dependencies it declares are bound the way an injected install binds them: to the version its parent provides. If the parent does not depend on the peer, pnpm looks further up, to the deployed project. A peer that no ancestor provides is bound to the deployed graph's own resolution. This includes optional peers declared only in peerDependenciesMeta.
The binding is ambiguous when the ancestors provide more than one package or version, or when no ancestor provides the peer and the deployed graph holds more than one version. The deploy then fails with ERR_PNPM_DEPLOY_AMBIGUOUS_PEER, naming the package, the peer, and the competing resolutions. Pin the peer to one version with an overrides entry, or turn injectWorkspacePackages on, which is the setting that decides between the candidates.
Before v12.2.0, the command refused every non-injected workspace up front. --legacy, or forceLegacyDeploy: true, still selects the older implementation.
注意
当设置 enableGlobalVirtualStore 选项时,pnpm deploy 忽略了它,并且总是在部署目录中创建一个本地化虚拟存储。这样可以保持部署目录的自包含性和可移植性。
使用方法:
pnpm --filter=<deployed project name> deploy <target directory>
The filter must select exactly one project. Selecting none fails with ERR_PNPM_NOTHING_TO_DEPLOY, and selecting several fails with ERR_PNPM_CANNOT_DEPLOY_MANY. A target directory that is unsafe to write, such as the workspace root, fails with ERR_PNPM_INVALID_DEPLOY_TARGET.
Relative file: paths in root overrides are resolved from the source workspace. Their dependencies are included in the deployed package.
如果你在部署之前构建项目,也可以使用 --prod 选项跳过 devDependencies 安装。
pnpm --filter=<deployed project name> --prod deploy <target directory>
在 Docker 镜像中的使用。在你的 monorepo 中构建完所有内容后,在第二个镜像中执行此操作,该镜像使用你的 monorepo 基础镜像作为构建上下文或在额外的构建阶段:
# syntax=docker/dockerfile:1.4
# The workspace image must use WORKDIR /app
FROM workspace as pruned
RUN pnpm --filter <your package name> --prod deploy pruned
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=pruned /app/pruned .
ENTRYPOINT ["node", "index.js"]
A deployed symlink to a directory inside the project is kept even if the directory is not deployed. So is a symlink to a missing path inside the project, such as a link into a removed node_modules. A link into node_modules resolves to the dependency installed in the output directory. A symlink to a file that is not deployed is replaced by a copy of the file.
To deploy without registry metadata, keep the source lockfile and populate the
store with pnpm install or pnpm fetch before running pnpm deploy --offline.
This also works with per-project lockfiles and --legacy. Use --trust-lockfile
if deployment should skip supply-chain policy checks on the locked dependencies.
New or changed dependencies may still require registry metadata.
Deployment preserves the recorded peer bindings of injected workspace packages,
including when using lockfile.format: v13.
配置项#
pnpm deploy accepts the options of pnpm install for the install it runs into the target directory.
--force#
Allow deployment to a non-empty target directory. Without this flag, pnpm fails
with ERR_PNPM_DEPLOY_DIR_NOT_EMPTY if the target directory is not empty.
--ignore-scripts#
Skip lifecycle scripts during deployment, including dependency build scripts.
See pnpm install --ignore-scripts.
--dev, -D#
仅安装 devDependencies。
--no-optional#
不安装 optionalDependencies。
--prod, -P#
devDependencies 中的软件包将不会被安装。
--filter <package_selector>#
--legacy#
强制实施原有部署实现。
默认情况下, pnpm deploy 将尝试从共享锁文件创建专用锁文件以进行部署。 The --legacy flag disables this behavior.
Since v12.4.1, the legacy implementation prefers the versions the source workspace lockfile pins, wherever they still satisfy the deployed project's ranges.
Since v12.16.0, it checks the source workspace lockfile against minimumReleaseAge and the other resolution policies first, as pnpm install does, and fails if one of them rejects a locked version. trustLockfile or --trust-lockfile skips that check.
What the deployed project gets#
- The
packageManageranddevEngines.packageManagerfields of the workspace rootpackage.jsonare not copied into the deployedpackage.json. - The virtual store is placed at
virtualStoreDir, resolved against the deploy directory. With the global virtual store enabled or an absolutevirtualStoreDir, it stays atnode_modules/.pnpm. - The
preparescript of the deployed project is not run. - A workspace project that the deploy installs as a
file:dependency runs its build scripts. pnpm approves it by its dependency path in theallowBuildssetting. AnallowBuildsentry that denies the project still applies. - A workspace project with
publishConfig.directoryis installed from that directory. Its build scripts run only if the directory is inside the project and not insidenode_modules.
Reproducible output#
Since v12.15.0, when the SOURCE_DATE_EPOCH environment variable is set, pnpm deploy and pnpm install use it as an upper bound for the timestamps in node_modules/.modules.yaml and node_modules/.pnpm-workspace-state-v1.json. With an equivalently prepared deploy target and an epoch no later than the timestamps that would otherwise be recorded, deploying the same project with the same lockfile produces identical files, so a Docker layer built from the deploy directory stays cached.
The next pnpm install or pnpm run in that directory checks the installed dependencies against the lockfile instead of trusting file modification times.
已部署项目中包含的文件#
By default, pnpm copies the files that pnpm pack would include. They are selected in one of the following ways, resolved in order:
- 项目的
package.json可能包含一个“files”字段来列出应复制的文件和目录。 - 如果应用程序目录中有一个
.npmignore文件,那么这里列出的任何文件都会被忽略。 - 如果应用程序目录中有一个
.gitignore文件,那么这里列出的任何文件都会被忽略。
To copy every file of the project, set deployAllFiles to true.
配置#
forceLegacyDeploy#
- 默认值: false
- 类型:Boolean
默认情况下, pnpm deploy 将尝试从共享锁文件创建专用锁文件以进行部署。如果此设置设为 true,则将使用旧的 deploy 行为。