Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

node.js - Difference between `npm link x` and `npm install /path/to/x`

I thought I understood the difference between

npm link x

and

npm install /local/path/to/x

originally I thought the former created a symlink to x, whereas the latter installed a separate copy of x in your project, instead of symlinking it.

However, I recently noticed that my original impression was wrong, and they both seem to use symlinks - so is there a difference between the two and what is it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An article on Medium by Alex Mills lays it out bare.

It says the difference between npm link x and npm install /local/path/to/x are:

  1. The big difference is that npm install /local/path/x will run the preinstall/postinstall hooks, but npm link x will not.

  2. npm link uses the global NPM space, npm install /local/path/x does not. npm link creates a symlink to x in the global space, and then when you call npm link x from y, it creates a symlink not directly to x, but rather to the global symlink. This is an important differences if you are using different global node.js versions, e.g., NVM.

  3. npm install /absolute/path/x will alter package.json, npm link x does not.

To get a fresh local copy instead of a symlink, use npm pack, like so:

tgz="$PWD/$(npm pack)"
cd <other project>
npm install "$tgz"

You could also use cp/rsync, but that wouldn't run install hooks or put the executables in node_modules/.bin...that will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...