8

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

Flion
  • 10,468
  • 13
  • 48
  • 68

4 Answers4

10

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.

Liam Baker
  • 116
  • 1
  • 3
  • it does thanks. I didnt see that article before, so its good to hear a confirmation that there is no other way. I may switch to the relative file path. Tnx! – Flion Sep 03 '18 at 15:23
  • 4
    I am absolutely baffled that such a basic use case (develop against a local version of a dependency but publish using the published version) seems to be totally unsupported by npm. Does everybody really just constantly hand-jam changes into their `package.json`?!? – Coderer Aug 22 '19 at 13:11
  • Good answer, Always do your `npm install` before your `npm link`! – Jesse Reza Khorasanee Oct 01 '20 at 00:16
  • This actually helped me understand whats going on. I have a package linked but after each npm install, the package throws errors because it apparently uses the one from the npm registry, so I have to npm install, link my package again and even have to go into the package and run npm install there – CodingKiwi Sep 04 '21 at 07:58
6

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>"
},
Endre Kántor
  • 61
  • 2
  • 2
1

OP here. I've recently switched to yarn, which is very compatible with npm. Yarn has a thing called workspaces which handles the whole linking issue much better. Havn't had any issues like the above since switching.

Flion
  • 10,468
  • 13
  • 48
  • 68
1

Deleting package-lock.json and then running npm link <package_name><local_package_path> fixed the error.

sh87
  • 1,063
  • 10
  • 12