pnpm: `pnpm deploy` doesn't rewrite `workspace:*` dependencies in `package.json`

pnpm version:

7.30.0

Code to reproduce the issue:

Run pnpm -F <project> deploy

Expected behavior:

When the target directory is created, I expected the package.json to be “updated” to include the workspace packages with their respective version numbers similar to pnpm pack or pnpm publish.

Actual behavior:

workspace:* versions remain in package.json. This in turn means consumers of the output directory can’t rely on npm/yarn or any other package manager. A temporary workaround would be to pnpm pack the target directory, then pull the “clean” package.json from the tarball, and put that back into the target directory of the deploy.

Additional information:

  • node -v prints: v18.12.1
  • Windows, macOS, or Linux?: any

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 14
  • Comments: 16 (6 by maintainers)

Most upvoted comments

It might be somewhat related, in my case since deploy doesn’t create a flat node_modules, the docker image cannot find packages that would be in the workspace root package.json dependencies. But strangely this only happens in docker, on local deploy seems to work fine, it’s probably more nuanced.

Point is all of this added complexity and headache could be entirely avoided if pnpm deploy created something npm compatible. That would be really nice.

Thanks @pkerschbaum! We ended up working around this by relying on the output of pack and pulling the “correct” package.json from that (in a Dockerfile):

RUN pnpm --recursive run build \
	&& pnpm --filter <package> deploy --prod pruned \
	&& cd pruned \
	&& pnpm pack \
	&& tar -zxvf *.tgz package/package.json \
	&& rm package.json \
	&& mv package/package.json package.json \
	&& rm *.tgz

+1 to this. It is a critical blocker to adopting pnpm in any organization that has multiple projects on yarn or npm, as simply trying to install local packages using portal: or links becomes impossible.

It appears that it’s a careless mistake which still remains in version 8.0.0-rc1. Nevertheless, it is not a significant issue since the code is capable of running without the need for package.json.

Nevertheless, it is not a significant issue since the code is capable of running without the need for package.json.

This really depends on your actual use case. In my case, it’s a major blocker that requires the workaround 🤷🏻

I believe this is addressed by https://github.com/pnpm/pnpm/pull/6943

Which is not released yet, but hopefully will be soon

I’m distributing an app through a Docker image that relies on pnpm deploy to generate the final “dist” that’s shipped. To keep the image lightweight, it’s “just” node18-alpine and our dist folder, that in turn also means that the user may or may not have to interact with the packages through npm instead of pnpm. Even though it’s recommended to enable pnpm with corepack in that case, that’s not always followed or feasible. When trying to use any npm command against the package (like npx to run the bin), it’ll fail as the package.json file is “invalid” in that context 👍🏻

Had the same problem, and solved it with a script which replaces the workspace: protocol of workspace dependencies by the respective version number.
Maybe it helps you as a workaround.

// replace-workspace-protocol-by-version.mjs
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import { Command } from '@commander-js/extra-typings';
import { findWorkspacePackagesNoCheck } from '@pnpm/find-workspace-packages';

const program = new Command().argument(
	'<pathtopackagejson>',
	'the path to the package.json of which the dependencies using the "workspace:*" protocol should be replaced by the versions present in the monorepo (e.g. "./deployed-package/package.json")'
);

program.parse();

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const projects = await findWorkspacePackagesNoCheck(path.join(__dirname, '..', '..', '..'));
// first elem of returned array is the root package.json, the monorepo itself --> discard that
const [_rootProject, ...workspaceProjects] = projects;

const absolutePath = path.join(process.cwd(), program.args[0]);

const packageJsonContent = JSON.parse(await fs.promises.readFile(absolutePath, 'utf-8'));

/**
 * @param {{[key: string]: string}} dependencies
 */
function replaceWorkspaceProtocolBySpecificVersion(dependencies) {
	if (typeof dependencies !== 'object' || dependencies === null) {
		return;
	}

	for (const [pkgName, pkgVersion] of Object.entries(dependencies)) {
		if (pkgVersion.startsWith('workspace:')) {
			const matchingWorkspaceProject = workspaceProjects.find(
				project => project.manifest.name === pkgName
			);
			if (!matchingWorkspaceProject) {
				throw new Error(
					`could not replace the workspace protocol of a package, reason: package not found! pkgName=${pkgName}`
				);
			}
			if (!matchingWorkspaceProject.manifest.version) {
				throw new Error(
					`could not replace the workspace protocol of a package, reason: package has no version! pkgName=${pkgName}`
				);
			}

			dependencies[pkgName] = `^${matchingWorkspaceProject.manifest.version}`;
		}
	}
}

replaceWorkspaceProtocolBySpecificVersion(packageJsonContent.dependencies);
replaceWorkspaceProtocolBySpecificVersion(packageJsonContent.devDependencies);
replaceWorkspaceProtocolBySpecificVersion(packageJsonContent.peerDependencies);
replaceWorkspaceProtocolBySpecificVersion(packageJsonContent.optionalDependencies);

await fs.promises.writeFile(
	absolutePath,
	JSON.stringify(packageJsonContent, undefined, 2),
	'utf-8'
);
# run it against the package.json of the deployed package
node ./replace-workspace-protocol-by-version.mjs ./deployed-package/package.json

Hmm, unfortunately #6943 has been reverted by https://github.com/pnpm/pnpm/pull/7058. Is there another solution? I’m also facing this problem in the context of docker images.

This really depends on your actual use case. In my case, it’s a major blocker that requires the workaround 🤷🏻

@rijkvanzanten Just curious. If you need to read the version of your dependencies at runtime, you probably want to get it like

import libPackageJson from 'lib_name/package.json';
const libVersion = libPackageJson.version;

Alternatively you can get the versions from the lock file.