NPM: After "npm link" module is not found
node.jsNpmnode.js Problem Overview
I'm developing two modules for NodeJS, first one named aligator
and second one aligator-methods
. Second one depends on first one to work. I'm developing these two modules at the same time and I want to global link aligator
so I can use it like it is on npm registry and I just installed it globally. To do this NPM documentation says that I need to use npm link
but it's not working.
File package.json
of module aligator
:
{
"name": "aligator",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "spec"
},
"scripts": {
"test": "gulp jasmine"
},
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"bluebird": "^1.2.4",
"lodash": "^2.4.1",
"mathjs": "^0.22.0"
}
}
File package.json
of module aligator-methods
:
{
"name": "aligator-methods",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "jasmine"
},
"scripts": {
"test": "gulp jasmine"
},
"author": "",
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"lodash": "^2.4.1",
"mathjs": "^0.22.0",
"aligator": "^0.0.1"
}
}
First of all I linked the module globally:
$ cd ~/aligator
$ npm link
/usr/local/lib/node_modules/aligator -> /Users/roc/aligator
This if I'm not mistaken has created a global reference of my module aligator
and now I can use this module from everywhere I want in the computer.
Then I went to the other module and tried to install the dependency but it gave me this output:
$ cd ~/aligator-methods
$ npm install
npm ERR! 404 404 Not Found: aligator
npm ERR! 404
npm ERR! 404 'aligator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404 It was specified as a dependency of 'aligator-methods'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.
npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/roc/aligator-methods
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.16
npm ERR! code E404
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/roc/aligator-methods/npm-debug.log
npm ERR! not ok code 0
I even tried to link it directly with:
$ cd ~/aligator-methods
$ npm link aligator
/Users/roc/aligator-methods/node_modules/aligator -> /usr/local/lib/node_modules/aligator -> /Users/roc/aligator
But it didn't work either.
Any thoughts on what it is that could be happening? I read somewhere that maybe it had something to do with my installation of node and npm because it was made by Homebrew and so sometimes I need to use sudo
, it seemed unlikely but I tried what they proposed and It didn't work either.
node.js Solutions
Solution 1 - node.js
The problem was that the main
property of package.json
was pointing to a non-existing file. It seems that the problem can happen due to multiple reasons so be sure to take a look at other answers.
Solution 2 - node.js
I ran into this issue because of NVM, I was running one version of node for the dependency and another for the dependant.
Solution 3 - node.js
Deleting package-lock.json
then running npm install
again resolved the issue for me.
Solution 4 - node.js
When you first run npm link
from the aligator
directory, you create a link from your global node_modules directory to aligator
. Then when you run the npm link aligator
from the aligator-methods
directory, you link aligator
from your locally installed node_modules to the original source (as the output shows in your example above). Once this is done, there shouldn't be a need to install anymore since it's already "installed". What errors are you seeing after you run the npm link aligator
command?
If you just want to install a dependency from a local directory, you might just try using npm install
instead. For example:
> $ cd ~/aligator-methods
> $ npm install ../aligator
Solution 5 - node.js
My issue ended up being that repo A was using npm
and repo B was using yarn
, so I needed to run yarn link
in repo B in order to pull it in via npm link package-name
into repo A.
Solution 6 - node.js
What worked for me was to:
- Delete the
node_modules
in both the dependency and the consumer module. - Run
npm unlink --no-save [dependency-module]
- re-link with the 2-link commands as per npm-link
Now I am able to fully test my unpublished module locally.
Additionally, there is an npm pack command which can help you test your unpublished modules, although not quite as robust.
Solution 7 - node.js
Fix for my version of this issue; in npm v5.3.0, I removed node_modules
from repo I was linking into another project.
I found out that after npm v3 they try to put all node_modules dependencies into one node_modules directory (one in your project) to flatten the structure as much as possible (http://codetunnel.io/npm-5-changes-to-npm-link/).
Solution 8 - node.js
For me this happened when I decreased the version number of my local package from 0.1.0 to 0.0.1. And in the projects where I linked to this package I was still using the higher version number. Updating dependencies in package.json
fixed it.
Solution 9 - node.js
Check tsconfig moduleResolution
If like me, you happened to change the tsconfig module
from es5
to esnext
or something, then the moduleResolution
default may have changed.
Without moduleResolution
being set to "node", typescript will not resolve node_modules packages.
You can read on the Compiler Options page about how the default value depends on the value of module
, whose default in turn depends on target
— but probably set it to "node" explicitly.
Solution 10 - node.js
I had a similar issue, and I had to perform the following steps to solve it:
In the library:
- Setup the libraries that are generating issues as
peerDependencies
in thepackage.json
instead ofdependencies
ordevDependencies
, e.g. in my casereact
:
"peerDependencies": {
"react": "^16.8.6",
...
}
- run
npm install
- build the library (in my case, with a
rollup -c
npm script)
In my main app:
- change the version of my library to point to my local project with a relative path in
package.json
, e.g.
"dependencies": {
"my-library": "file:../../libraries/my-library",
...
}
-
Add
resolve.symlinks = false
to my main app's webpack configuration -
Add
--preserve-symlinks-main
and--preserve-symlinks
to mypackage.json
start script, e.g:
"scripts": {
"build": "set WEBPACK_CONFIG_FILE=all&& webpack",
"start": "set WEBPACK_CONFIG_FILE=all&& webpack && node --preserve-symlinks-main --preserve-symlinks dist/server.js",
}
- run
npm install
- run
npm run start
Solution 11 - node.js
Be sure to check the main
in package.json
.
This serves as the entry of your package. This is a slight detail that took me a long time.
Solution 12 - node.js
I know this is an old post, but in my case the issue was that I had renamed my package directory name, but the package.json
"name" was still set to the old name.
for example, my directory name was package-name
but the actual "name" found in package.json was package-name-b
".
running yarn link
would create a link called "package-name-b".
I then tried to run yarn link package-name
since I was using the directory name. When I switched it to yarn link package-name-b
, it worked.
Solution 13 - node.js
May be trivial, but worth mentioning:
npm link <module-name>
must be executed after npm install
(if needed) was executed in the <module-name>
folder.
- That is, unless the linked module is already present in
<module-name>
folder'spackage.json
, in which case the order won't matter, because it means that the linked module is actually installed solely bynpm install
(as demonstrated here), and there's no need for linking usingnpm link
- which is not the subject of this question.
The reason is that npm link <module-name>
simply creates a symlink (or a folder shortcut, in Windows) to the linked package, so that executing npm install
afterwards just deletes it.
To summarize, this is the order of execution:
- Replacing the OP
aligator
withexporter
andaligator-methods
withimporter
for easier grasp
⚡ cd exporter
⚡ npm install <-- if needed, execute here, though it can also be executed after `npm link`
⚡ npm link
⚡ cd importer
⚡ npm install <-- if needed, must be executed here
⚡ npm link exporter
- Bonus: A full minimal ES modules example of exporter and importer can be found here.
Solution 14 - node.js
When using peerDependency
I'm developing two packages, stejs
, and stejs-loader
. stejs-loader
has stejs
as a peerDependency
. When I ran npm link stejs-loader
and npm link stejs
in my project I was getting an error that stejs-loader
couldn't find stejs
. I got it fixed by running npm link stejs
in the directory of stejs-loader
.