How to use published version of a package in an NPM workspace mono-repo

187 views Asked by At

I have a Node Typescript Mono-repo with 2 packages in it. One package is a library which gets published to an npm artifactory. The other package is an express service.

I want the service to list the library as a dependency: "xyz-library": "^1.0.0",

The problem I'm having is when using and importing the library in the service package, it seems to be referencing or installing the latest code of the library to the node_modules package, instead of grabbing version 1.0.0. For example if I delete a file in the library that is being referenced in the api, now I am given import errors (which I shouldnt because that file should technically still be present in version 1.0.0).

It seems this is the default configuration of an NPM workspace and working as expected using links to connect the code. However I was wondering if I can configure the service to use the published version of the library instead?

1

There are 1 answers

0
Eric Andow On

EDIT: I think I found a solution to this specific situation, although it doesn't solve things in the general case.

Pin the dependency to an old version. If you run npm i [email protected] --save-exact in the service, that should force npm to install the old version of "xyz-library", which it can only get from the repository.

You'll know if it worked if it installs "xyz-library" in a node_modules folder next to the service package.json file.

ORIGINAL ANSWER:

I'm having this same issue, and I haven't been able to find a good answer using npm workspaces. I tried all the options for the --install-strategy flag, but it didn't change this behavior.

As far as I can tell, you can do one of the following:

  1. Use a different package manager, like pnpm or yarn.
  2. Stop using workspaces. Dependencies will be installed next to the package.json file that specifies them, in exactly the way they're specified.
  3. Accept this behavior as-is.