Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 914
Alexander Mills Avatar asked Jun 04 '18 05:06

Alexander Mills


People also ask

What is the difference between npm install and npm install?

npm install , or npm i , is used to install dependencies: It will install all the dependencies. If you use ^ or ~ when you specify the version of your dependency, npm may not install the exact version you specified. npm install can update your package-lock.

What is npm link used for?

npm link is an npm command which, when used correctly, creates a link between projects to facilitate development in a local environment. This allows you to reference my-package locally (instead of the npm-hosted package) without modifying package. json in my-project.

Where is npm link stored?

npm link installs the package as a symbolic link in the system's global package location ('/usr/local/lib`). This allows you to test the package while still developing it, without having to install it over and over again.

How do I link npm modules?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.


2 Answers

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.

like image 108
Alexander Mills Avatar answered Oct 05 '22 18:10

Alexander Mills


npm link

npm link
npm link <folder>

Both of the above command will create a symlink of the <folder> in the global packages.

Now npm link <folder> will symlink the same in your node_modules folder also for your current project. And these names would be based on project name in package.json and not based on the folder name you are linking

The package.json of your current project will not be touched or altered

The dependencies of the package will still be installed as you can see in the code here

https://github.com/nodejs/node/blob/31d5bdea70e44802918d6f4aa7c378bc1992be54/deps/npm/lib/link.js#L156

So to summarize:

  1. It creates a symlink in the global folder (always).
  2. It doesn't alter the package.json.
  3. It does install any of missing dependencies.

npm install

Now npm install <folder> is a bit different to this:

  1. It doesn't create a symlink in the global folder.
  2. It alters and adds the reference to package.json.
  3. It creates a symlink to original folder.
like image 21
Tarun Lalwani Avatar answered Oct 05 '22 20:10

Tarun Lalwani