Search code examples
node.jsnpmdependenciesregistry

Best way to manage temporary local dependency changes in dev for npm dependencies


In maven, if module A depends on Module B. When you change B locally, you can easily use mvn install (for B) to deploy B into the local repo, so A can get the updated B without downloading from the remote repo. This is pretty efficient.

However, in NPM world, I can't find the equivalent. I have package A depends on B, during dev time, I'm frequently changing B, I definitely don't want to run npm publish for every single change. That's slow and doesn't make any sense to publish partially finished package B to the public.

I know NPM supports local dependency, but that way has few drawbacks:

  1. You have to change package.json every time you switch to dev mode and remember to change back to official dependency when it's ready. That's very tedious and error-prone.
  2. Even with local dep, you have to run npm build for B for packaging and run npm upgrade for A, that's a very painful and slow process

I want to find an efficient way for dev process to meet the following requirements:

  1. No changes on package.json when switching to dev mode.
  2. Ideally, no additional steps are needed for A to see changes of B, whenever the source file is changed in B, A should see the update immediately.
  3. If 2 is not possible, at least, the process should be fast and frictionless.

My idea is there should be a simple NPM registry dev proxy, During dev time, NPM connects to this proxy. By default, it downloads packages from the upstream registry. We can config it, so for particular dependencies e.g. B, it will download from specified directory e.g. local B's dist directory. Thus, we never touch A's package.json.

Is there any similar solution or any other better suggestion on this problem?


Solution

  • npm link seems to do most of what you're asking.

    You don't have to change package.json - it doesn't affect it in any way. After you've run npm link once, all changes to the linked package will be seen by your package instantly. Because it only changes your node_modules folder, as long as you aren't carrying that from dev to remote (you shouldn't be committing it to version control anyway), there are no changes back that you need to remember.

    You may still have to run npm link path/to/linked/package every time you change to dev mode, but that's a fairly painless command which doesn't get anything from remote.