1

Let's say you have a monorepo with a module federation, with 5 different apps that you want to build in separate CI steps and you need to have all node_modules prior to executing the build of each app. With npm ci taking ~1min, that is 5 additional minutes for the whole CI, even with --prefer-offline and an .npm cache... It seems the main time-consumers of the installation are the dependency-checks with package-lock and not the download-times, the .npm-cache is really not making much of a difference, maybe just 15 seconds.

Instead of caching $HOME/.npm, I am trying to cache the ready-to-use node_modules folder. In a monorepo, you need to cache the non-hoisted node_modules in the workspaces too, but you can get a reference to them via npm query .workspace | jq -r '.[].location', zip them all together and upon unpacking the cache, just put everything back into the respective subfolders/workspaces.

Now my problem is, you also need to link the workspaces in node-modules, e.g node_modules/@my-company/some-lib. npm ci does that automatically, but it also checks/installs external dependencies. Is there any way around this, to make npm only do the linking?

Are there any other things which would make this unstable? Keep in mind I already take care of versioning. I don't use package-lock.json for invalidating the cache but rather a package-lock.json-checksum without workspace-versions, so only externally installed packages matter.

Has anyone else tried this approach?

Phil
  • 7,065
  • 8
  • 49
  • 91
  • just fyi, it seems NX has a way of doing the linking in a monorepo instead of relying on a package-manager. See the package.json in the example without any workspace-definitions: https://github.com/nrwl/nx-recipes/blob/main. I will try their integrated repo approach – Phil May 29 '23 at 10:03

0 Answers0