NPM can be used for different things

  • front-end projects where the final artifacts often have all their dependencies included.
  • service side services where the node_modules directory is available to the runtime server code.

NPM currently hoists dependencies to the root node_modules folder in a workspace project. Even their new proposal https://github.com/npm/rfcs/blob/main/accepted/0042-isolated-mode.md still doesn't seem to allow for all dependencies to be installed just alongside the node_modules folder next to the package.json for that workspace folder. You either have to copy the root node_modules folder to a parent directory, which gets complicated releasing multiple services (since it's a shared resource), or you must merge the two which is also not trivial.

This becomes a problem when you want to build in a CI environment and then move the code and all dependencies somewhere else

Does anyone have thoughts on how they've solved/avoided/approached this problem?

2

There are 2 answers

0
rubixibuc On

I ended up going with Lerna, since it seems to handle this situation perfectly. Lerna basically treats every "sub-project" independently.

https://lerna.js.org/

I don't think npm workspaces fits this type of workflow, or it wasn't designed to be in the first place

0
Ryan Wheale On

If you are working in a monorepo, I highly advise pnpm because it is very fast, similar-ish to npm (it will feel familiar), and has had first-class support for monorepos since day 1 (unlike npm and yarn). Most relevantly, it installs dependencies at the project level like you want - thus avoiding all the problems that come from hoisting.

I also recommend a tool like turborepo or nx for powering your monorepo (nx is now the engine behind lerna).