Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm install after npm link-ing local module causes error: Not found

package.json of module-A has module-B listed as dependency

  "dependencies": {
    "@mynamespace/module-b": "^0.0.1",

module B is a local module and is linked successfully from module A with npm link. Compiling / running things all goes good and well.

However when I try to install any new module in module A with npm install something or just run npm install or npm uninstall something I always get the error from npm that the local module (which is npm link-ed) is not found.

C:\web\module-b>npm install
npm ERR! code E404
npm ERR! 404 Not Found: @mynamespace/module-b@^0.0.1

I checked the main property in package.json in both modules as suggested here. There are several similar questions, but none seem to be exactly this problem or give a solution that works.

Right now I'm manually removing all mentions of linked modules from package.json, then I run npm commands, and than I add them back to package.json

Im using npm 6.1.0

Edit: Ah, this might be crucial? @mynamespace/module-b does not exist yet in NPM registry, only locally

like image 991
Flion Avatar asked Aug 01 '18 19:08

Flion


2 Answers

Right now I'm manually removing all mentions of linked modules from package.json, then I run npm commands, and than I add them back to package.json

Unfortunately this is the only way this can work. npm install will always search the npm registry if you only specify a version (i.e "@mynamespace/module-b": "^0.0.1", or "*") so running npm install will override what you have in the node_modules of your project with what it finds on the npm registry (or throw a 404 in this case).

Assuming you've read this article, there is no way to use the npm link method and also run npm install. For this you'll have to explicitly write the path to the local package in yourmain project's package.json (and then change it back when you've published your package).

"dependencies": {
  "@mynamespace/module-b": "file:../../module-b",
},

I hope this helps.

like image 106
Liam Baker Avatar answered Nov 17 '22 11:11

Liam Baker


Npm can do it automatically if you add an "install" script to package.json, which runs right after npm install.

"scripts": {
    "install": "npm link <your package>"
},
like image 31
Endre Kántor Avatar answered Nov 17 '22 10:11

Endre Kántor