How can I add an existing directory to a pnpm workspace while preserving its existing package locks?

469 views Asked by At

Say I have a repository with packages/foo/package.json and packages/foo/pnpm-lock.yaml. I want to add packages/foo to a pnpm workspace at the repository root.

As far as I can tell, when I add packages/foo to pnpm-workspace.yaml and run pnpm install, pnpm ignores the existing packages/foo/pnpm-lock.yaml file and re-resolves the entire transitive dependency tree of packages/foo/package.json from scratch. It's important for the stability of this package that this not happen; many things break if this package's transitive dependencies receive an unlocked upgrade.

How can I get pnpm to preserve the versions from packages/foo/pnpm-lock.yaml when building the workspace's pnpm-lock.yaml?

1

There are 1 answers

1
A_mohii_ On
  1. Add Your Package to the Workspace: Just manually add your package to the pnpm-workspace.yaml file. Something like this will do:

    packages:
      - 'packages/*'
    
  2. Run pnpm install: Do this from your workspace root. pnpm is pretty smart about workspaces and should respect your existing pnpm-lock.yaml in most cases.

  3. Check After Installation: Once you run pnpm install, take a peek at your workspace's pnpm-lock.yaml. It should now include your package's dependencies, ideally matching what you had in your package's lock file.

  4. Handle Any Conflicts: If things look off (like some versions didn't match), you might have to deal with version conflicts manually. This can happen if other packages in the workspace have conflicting dependency requirements. Remember, pnpm doesn't have a magic command to do this directly, but it usually handles workspace dependencies well.

Happy coding!