node.jsnpm

How do I specifically use an NPM local dependency for local development and one from an external registry for production?


I am working on a suite of tools that all use code from the same library. The library is also developed in-house.

When I publish a tool, I want it to use a specific version of the library from the NPM registry (npmjs.com) but when I develop it locally, I want it to specifically use a relative local package (i.e.: ../some_package). It seems like I can do this with different environment variables and scripts in package.json but that seems very roundabout.

The part that matters in one of these tools looks like this:

"dependencies": {
  "@teaminkling/autolib": "^0.1.1"
},

...which is fine and works for production. However, to work on it locally and still have version control, I've needed to do this:

"dependencies": {
  "@teaminkling/autolib": "file:../autolib"
},

I would expect this question be a duplicate since I think it's a pretty common development pattern, but I couldn't find anything after a while. I also welcome comments about better ways to handle this kind of library-dependency management.

Thanks for the help!


Solution

  • Run npm link inside the autolib source, then run npm link @teaminkling/autolib in any package source that you want to use the development version with. Your package.json should still refer to the real versions for production, but they will be ignored for locally linked packages.