Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`npm link --save` does not update dependencies on my package.json

Tags:

node.js

npm

I am using npm link package --save to create a local link to a globally installed package.

It correctly creates the link to the package (and would install it globally in case it were not already installed); but it fails to update the dependencies in package.json.

What I am missing here?

like image 591
PA. Avatar asked Feb 22 '15 23:02

PA.


2 Answers

According the npm docs, npm link is not intended to change your package.json. It creates symbolic links on your file system for a package.

This allows you to still reference a module by name, but have it pull from your local filesystem:

cd ~/projects/node-redis    # go into the package directory
npm link                    # creates global link
cd ~/projects/node-bloggy   # go into some other package directory.
npm link redis              # link-install the package

If you actually intend to insert a file path in your package.json, use npm install instead:

npm install --save /path/to/package

Then you'll see a reference in package.json file:

"dependencies": {
  "local-package": "file:/path/to/package"
}

Though I highly recommend you use npm link instead, as it makes your package.json more portable. If you commit your changes with local file paths, it may become invalid on another system, or if you move around the files.

like image 172
Steven Schobert Avatar answered Oct 25 '22 04:10

Steven Schobert


Update: Use npm-link-better


I created a little tool for this: npm-link-save

npm-link-save

npm-link with --save (or --saveDev) to save the linked dependency in your package.json.

You can also link multiple dependencies with this.

Install

npm i -g npm-link-save

Usage

npm-link-save express
# or
npm-links express
npm-links -D express // links in devDependencies
npm-links express morgan // multiple links
like image 45
laggingreflex Avatar answered Oct 25 '22 06:10

laggingreflex