Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get npm to favor local linked dependency over its published install

I've searched through other questions such as this one, but they all seem to be about a local npm link stopping working for another reason than mine. I assume this is a common use-case issue, so if I'm doing something methodically wrong, I'm more than happy to take suggestions on how I should be doing it.

Principally, I have a private npm module that I'm working on called @organisation/module. When working locally, I'll run npm link on it, and use it within my 'host' project as npm link @organisation/module — this all works great with hot-reloading, etc. I'll also import it as import module from '@organisation/module.

However, since I also want to publish my local changes to npm (as @organisation/module) from time to time, for build testing and production code, I need to run npm install @organisation/module on the host project.

This then seems to break the implicit npm link I set up earlier... I assume mainly because they are the same name, and npm favors an install over a link?

When I want to make live, local changes again, the only way I can currently get it to work is via npm uninstall @organisation/module and then to re-link it.

Is there a way to keep the published module installed (in order to avoid careless mistakes, like forgetting to reinstall it for build testing), but always favour the local, linked instance?

Diagram for ref: Diagram for ref

like image 399
Sarreph Avatar asked Sep 20 '18 11:09

Sarreph


People also ask

How do I fix dependency issues in npm?

The easy fix is to use the npm audit --fix which will look for updates that can be updated to fix those automatically.

How npm install specific dependencies?

Summary. For npm install specific version, use npm install [package-name]@[version-number]. Use npm view [package-name] version to know the specific latest version of a package available on the npm registry. Use npm list [package-name] to know the specific latest version of an installed package.


1 Answers

Have you tried locally installing with the other method npm provides.

npm install /absolute/path/packageName

I believe this will change your entry in package.json to look like this:

"dependencies" {
    ...
    "packageName": "file:../../path/to/packageName",
    ...
}
like image 59
AndrewSteinheiser Avatar answered Oct 26 '22 09:10

AndrewSteinheiser