pnpm deploy
从工作空间部署软件包。在部署期间,已部署软件包的文件将被复制到目标目录。已部署包的所有依赖项,包括来自工作区的依赖项,都安装在目标目录的独立 node_modules 目录中。目标目录将包含一个可移植包,可以无需其他步骤将其复制到服务器并执行。
注意
Since v11.26.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 v11.26.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>
如果你在部署之前构建项目,也可以使用 --prod 选项跳过 devDependencies 安装。
pnpm --filter=<deployed project name> --prod deploy <target directory>
在 Docker 镜像中的使用。在你的 monorepo 中构建完所有内容后,在第二个镜像中执行此操作,该镜像使用你的 monorepo 基础镜像作为构建上下文或在额外的构建阶段:
# syntax=docker/dockerfile:1.4
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.
配置项#
--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 将尝试从共享锁文件创建专用锁文件以进行部署。 --legacy 标志禁用此行为并且还允许在没有 injection-workspace-packages=true 设置的情况下使用 deploy 命令。
已部署项目中包含的文件#
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.
Since v11.28.0, symlinks that point to files or directories included in the deployed project are kept as symlinks.
The deployed project#
Since v11.28.0:
- The
packageManageranddevEngines.packageManagerfields of the workspace rootpackage.jsonare not copied into the deployedpackage.json. Versions v11.28.0 through v11.28.x copied them. - The virtual store is created at
virtualStoreDir, resolved against the deploy directory. A deploy from a shared lockfile recordsvirtualStoreDirin the deployedpnpm-workspace.yaml. With the global virtual store enabled or an absolutevirtualStoreDir, the deploy usesnode_modules/.pnpm. - The
preparescript of the deployed project is not run. --package-import-methodpassed on the command line is respected.
Since v11.28.12, a workspace project that the deploy installs as a file: dependency runs its build scripts. pnpm approves it by its dependency path in the allowBuilds setting. An allowBuilds entry that denies the project still applies. A project with publishConfig.directory is installed from that directory, and its build scripts run only if the directory is inside the project and not inside node_modules.
配置#
forceLegacyDeploy#
- 默认值: false
- 类型:Boolean
默认情况下, pnpm deploy 将尝试从共享锁文件创建专用锁文件以进行部署。如果此设置设为 true,则将使用旧的 deploy 行为。