145

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.

Roc
  • 2,500
  • 2
  • 15
  • 18
  • In the posted code, the name of the first module is spelled `aligtor` and you try to refer to it in the second module as `aligator`. That may also make your dependency crash. – Bruno Toffolo Mar 23 '16 at 18:41
  • @BrunoToffolo Yeah, you are right, but, in this case, this was just a misspelling in the post. I have corrected it, thanks. – Roc Mar 24 '16 at 11:03
  • lost 4 hours of my miserable life tricking webpack config :/ You saved my life! +1 – Tomas Ramirez Sarduy Apr 05 '17 at 22:22
  • 9
    Wow I had the same problem with the `main` of my `package.json`, thank you for updating the answer with your fix! – mattyb Jun 11 '17 at 21:42
  • if you have found the answer, it would be a good idea to post that as answer and set the question as resolved with that one :) – Alberto S. Jan 28 '20 at 17:20
  • @Alberto good idea! I just did! – Roc Jan 29 '20 at 09:56
  • My link script links 8 modules and has been working on Win10 and Ubuntu 19.10, Nodejs 10.22.0 and NPM 6.14.6. After moving to the latest Ubuntu 20.04 and nodej 16.9.1, NPM 7.21.1 , I no longer am able to link. I have tried all suggestion in this thread with no success. NPM -g list shows packages are linked globally in /usr/lib but multiple links to package_name only creates sym_link to the last package on the list of commands with no errors. All main property of the packages have proper value. I deleted all the `package_locks` as well as `node_module` folders. – E.Z. Oct 03 '21 at 19:58

18 Answers18

77

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.

Roc
  • 2,500
  • 2
  • 15
  • 18
  • 1
    Interesting to know that the project requires `main`. Mostly I did without it, but guess it creates these minor issues. – cst1992 Jun 30 '20 at 12:30
  • 2
    I had the same issue with a typescript project. `main` was referencing a lib folder that didn't exist. I just had to run build inside the project to generate the files. – Alesso Feb 16 '21 at 17:04
  • 1
    Also, `main` may be pointing to an existing file (typically, `dist/bundle.js`), but whose build was somehow broken so nothing is exported. – OfirD Aug 29 '21 at 19:01
70

I ran into this issue because of NVM, I was running one version of node for the dependency and another for the dependant.

linuxdan
  • 4,476
  • 4
  • 30
  • 41
47

Deleting package-lock.json then running npm install again resolved the issue for me.

  • 4
    That could resolve the current issue but possibly creates some larger ones. lock files play a very important role and _should not_ be deleted. In short: its the mechanisms that ensures that each teammember is using the exact same dependencies. You can checkout this answer on stack overflow: https://stackoverflow.com/questions/54124033/deleting-package-lock-json-to-resolve-conflicts-quickly But reading through the reason it exists in the docs is a good start as well. https://docs.npmjs.com/files/package-lock.json – SKuijers Sep 06 '19 at 06:16
  • 2
    I'd be willing to uptick this answer if there was a note in bold indicating that this should be a totally last resort. As @SKuijers points out, lock files play an essential role in maintaining dependency versions. Presumably, the dependency versions have been locked down in the `package.json` as well, but most of the time I see that the `package-lock.json` or `yarn.lock` have been the gatekeepers for this. – TheScrappyDev Jan 06 '20 at 22:04
  • When your dependency manager is incapable of managing dependencies. This is entirely valid. – MrYellow Apr 19 '23 at 23:13
  • So much winning here ♥️, solved my problem! – chrismarx Jul 12 '23 at 14:47
20

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

dylants
  • 22,316
  • 3
  • 26
  • 22
  • 8
    Thanks for your effort to resolve this. My `npm link` didn't show any errors. The problem in my case was taht the property `main` was pointing to a non-existing file. As for my `npm install` you're right, I didn't need to install anything the `npm link` does it all. Thanks for that I didn't know it. – Roc Jul 13 '14 at 17:42
  • 1
    I am having the same problem, but have not found a solution... if I try to individually require each linked package, all but one works... the one that does not work just says: "Error: Cannot find module 'module-i-just-linked'". – Michael Feb 19 '17 at 05:13
  • @Michael looks like I had a module nested in a deeper directory that was trying to "dynamically" require the module that was failing (i.e. the name of the string passed to require() was passed to the module), therefore I had to npm link into the deeper directory. – Michael Feb 23 '17 at 03:16
13

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.

TheScrappyDev
  • 4,375
  • 2
  • 21
  • 25
9

What worked for me was to:

  1. Delete the node_modules in both the dependency and the consumer module.
  2. Run npm unlink --no-save [dependency-module]
  3. 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.

npm-pack

ishaan
  • 1,951
  • 1
  • 19
  • 31
SandeepJ
  • 123
  • 1
  • 7
7

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.

munanadi
  • 799
  • 9
  • 13
5

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/).

crollywood
  • 523
  • 1
  • 6
  • 17
2

I had a similar issue, and I had to perform the following steps to solve it:

In the library:

  1. Setup the libraries that are generating issues as peerDependencies in the package.json instead of dependencies or devDependencies, e.g. in my case react:
"peerDependencies": {
  "react": "^16.8.6",
  ...
}
  1. run npm install
  2. build the library (in my case, with a rollup -c npm script)

In my main app:

  1. 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",
  ...
}
  1. Add resolve.symlinks = false to my main app's webpack configuration

  2. Add --preserve-symlinks-main and --preserve-symlinks to my package.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",
}
  1. run npm install
  2. run npm run start
Gerardo Roza
  • 2,746
  • 2
  • 24
  • 33
2

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.

Sam
  • 185
  • 1
  • 3
  • 14
1

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.

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

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.

Matthias
  • 13,607
  • 9
  • 44
  • 60
1

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's package.json, in which case the order won't matter, because it means that the linked module is actually installed solely by npm install (as demonstrated here), and there's no need for linking using npm 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 with exporter and aligator-methods with importer 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.
OfirD
  • 9,442
  • 5
  • 47
  • 90
0

I ran npm run build on the local package (dependency) and that worked for me

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Shubham Kundu
  • 185
  • 1
  • 10
0

I had encountered the same problem and tried all the solutions mentioned above, but unfortunately, none of them worked. After some investigation, I realized that the node version where I ran npm link [package-name] was different from the node version specified in the directory of my package when I ran npm ls -g --link. @_@ So, I made sure to match the node versions, and it worked! XD

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34334933) – shapiro yaacov May 10 '23 at 11:12
0

I had the same issue and found out the dist/cjs/index.js file which main was referring is missing. Running npm run build solved the issue.

Chinthaka
  • 343
  • 3
  • 13
-1

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.

ItsaMeTuni
  • 435
  • 4
  • 15
-1

I was unable to import from my linked package because I simply forgot to prepend ./ to my module exports in my top level index.ts file:

export * from './utilities'

Took a while to figure that one out.

Roman Scher
  • 1,162
  • 2
  • 14
  • 18