Roc
Roc

Reputation: 2719

NPM: After "npm link" module is not found

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.

Upvotes: 156

Views: 154460

Answers (22)

Freestyle09
Freestyle09

Reputation: 5528

For Angular I had to do one extra step to satisfy Typescript.

tsconfig.json file

    "paths": {
      "my-package": ["./node_modules/my-package"]
    }

Then I had to manually import my component from the given path.

import { MyComponent } from 'my-package';

IMPORTANT!

Use yarn link when you're using yarn as your package manager

Upvotes: 1

Mike T
Mike T

Reputation: 614

For me, I was experiencing this issue and the reason was that my npm linked package was using parcel and had the main property set in the package's package.json. This caused the parcel build to seemingly succeed, but with no files created in dist. Changing main to default in the package's package.json file fixed this for me.

Also, fixed via looking at this issue: Parcel is not creating a dist folder when I use build command

Upvotes: 0

Aamir khan
Aamir khan

Reputation: 1

I had the same issue with a typescript project. main was referencing a lib folder not the package folder that didn't exist. I just had to npm run build inside the dependent project which created the lib folder solved this problem.

Upvotes: 0

Alex Ink
Alex Ink

Reputation: 116

What solved my issue:

  1. In package: rename package in package.json.
  2. In package: npm run build to build a package.
  3. In package: npm link.
  4. In main client: npm link @name-of-my-package-from-renamed-package-json
  5. Restart VScode or whatever you use.
  6. Enjoy.

How to see changes:

  1. In package: npm run build
  2. Enjoy

Upvotes: 0

Chinthaka
Chinthaka

Reputation: 343

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.

Upvotes: 0

SanjaC23
SanjaC23

Reputation: 1

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

Upvotes: 0

Roman Scher
Roman Scher

Reputation: 1522

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.

Upvotes: -1

Shubham Kundu
Shubham Kundu

Reputation: 192

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

Upvotes: 0

Sam
Sam

Reputation: 184

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.

Upvotes: 2

munanadi
munanadi

Reputation: 1079

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.

Upvotes: 8

OfirD
OfirD

Reputation: 10510

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.

Upvotes: 2

Gerardo Roza
Gerardo Roza

Reputation: 3394

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

Upvotes: 2

Matthias
Matthias

Reputation: 15475

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.

Upvotes: 1

ItsaMeTuni
ItsaMeTuni

Reputation: 475

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.

Upvotes: -1

Roc
Roc

Reputation: 2719

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.

Upvotes: 88

TheScrappyDev
TheScrappyDev

Reputation: 4963

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.

Upvotes: 15

SandeepJ
SandeepJ

Reputation: 153

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

Upvotes: 11

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

Upvotes: 49

crollywood
crollywood

Reputation: 563

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

Upvotes: 5

linuxdan
linuxdan

Reputation: 4864

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

Upvotes: 80

Flion
Flion

Reputation: 10932

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.

Upvotes: 1

dylants
dylants

Reputation: 23440

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

Upvotes: 20

Related Questions