What is the optional version used for in `npm link <pkg>[@<version>]`?

Viewed 468

My understanding of what npm link and npm link <pkg> do is basically as described in the documentation:

Package linking is a two-step process.

First, npm link in a package folder will create a symlink in the global folder {prefix}/lib/node_modules/ that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name}. Note that npm link uses the global prefix (see npm prefix -g for its value).

Next, in some other location, npm link package-name will create a symbolic link from globally-installed package-name to node_modules/ of the current folder.

So I am puzzled by what it would mean to specify the version as well in the second step of package linking (for example, npm link my-package@3.1.4). Isn't the version of the linked package entirely determined by the contents of the linked directory? I don't think the documentation for npm link explains this.

2 Answers

From what i understand , npm link also does install any of missing dependencies. It ensures all latest versions of dependencies are linked to global folder and non latest are installed on spot , but with its deep dependencies located in its own node_modules . Installs or updates given project dependencies. If dependency version is not specified then one from package.json or latest is assumed. So if the dependency version is not specified , It will be entirely determined by the contents of the linked directory.

There is two different behaviour to know with the link command.

The first one, is when you want to test a local package, then in fact running npm link ../../my-package will be sufficient and will determined the version from the package.json file located in the package.

The second one, correspond to a case where you would like to tests a specific version of a remote package without impacting your package.json.

let say, you want to install moment@2.29.2 without updating your package.json then you can run

npm link moment@2.29.2

it will install the package globally and then create a symlink in your node_modules for that package.

Related