Flion
Flion

Reputation: 10902

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

Upvotes: 9

Views: 13539

Answers (4)

sh87
sh87

Reputation: 1133

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

Upvotes: 1

Flion
Flion

Reputation: 10902

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.

Upvotes: 1

Endre K&#225;ntor
Endre K&#225;ntor

Reputation: 71

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>"
},

Upvotes: 7

Liam Baker
Liam Baker

Reputation: 126

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.

Upvotes: 11

Related Questions