Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm link multiple local (inter-dependant) modules

I have 3 node.js modules, A, B and C. All of them private git repos. A depends on B depends on C. Git cloning A and doing npm install works like a charm.

But while coding on module A, i want to work on B (and C) as well. The latter two are git cloned too. And npm link ../pathto/B works well.

And as B depends on C, npm link took care of "installing" C into B/node_modules/C. Its a static file clone, that's being used by B.

So when doing npm link ../pathto/C, it results in A/node_modules/C (being a symbolic link).

But, and thats the problem, B will use its static clone of C, rather than what i have linked into A/node_modules/C.

A/
    ...
    node_modules/
        B -> B/
        C -> C/
B/
    ...
    node_modules/
        C/
            ...
C/
    ...

Does anyone have an idea to work around this?

like image 619
spaxxUnited Avatar asked Aug 23 '14 20:08

spaxxUnited


People also ask

Does npm link install dependencies?

In npm v6 the dependencies of a local linked package are installed. This means that if app has my-local-pkg as a dependency, running npm install generates app/node_modules folder and also my-local-pkg/node_modules (with its dependencies).

How install npm with all dependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

How install multiple modules npm?

The npm install command is used to install npm packages to a node_modules/ folder. You can also install a specific version of each package by adding the @version keyword after the package names. npm will install the exact versions you defined in the example above.


2 Answers

I solved it, or at least i got it working.

After npm install i do npm link _node_modules/* (_node_modules is the directory where my local modules B and C reside in).

So far B gets required as planned. But still B loads its static C reference.

Then i simply cd to _node_modules/B/node_modules and perform npm link C.

like image 104
spaxxUnited Avatar answered Sep 25 '22 08:09

spaxxUnited


For me, a simple

npm link A B C

works. Using npm 7.5.2

Of course, the packages have to be exposed prior, so

sudo npm link

must be run within each package's directory.

This does require a bit of gymnastics with package.json: during installations of new packages, dependencies on local modules must be hidden from requirements (or probably specified as paths, but I don't have sufficient knowledge for that yet). After that, the local packages need to be linked again. They must also ALWAYS be linked using one single command or npm will simply remove the missed ones.

Update 2021-11-08:

I'm using Vue and the above solution resulted in multiple Vue instances in my builds. It seems each of the linked modules disregarded the "normal" way of importing modules and did their own imports separately.

So now I'm using npm pack in the libraries and specifying dependencies in using projects as file dependencies "my_package_name": "file:/tmp/my_package_name-x.y.z.tgz",

like image 32
velis Avatar answered Sep 25 '22 08:09

velis