node.jsnpmnpm-installnpm-link

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?


Solution

  • 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.