且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

`npm link x` 和 `npm install/path/to/x` 之间的区别

更新时间:2022-12-08 09:44:14

Alex Mills 的一篇关于 Medium 的文章 将其展示出来.

它说 npm link xnpm install/local/path/to/x 之间的区别是:

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

  1. 最大的不同在于 npm install/local/path/x运行 preinstall/postinstall 钩子,但 npm link x 不会.

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

npm link 使用全局 NPM 空间,npm install/local/path/x 没有.npm link 创建一个指向 x 的符号链接在全局空间中,然后当您从y,它创建的符号链接不是直接指向 x,而是指向全局符号链接.这是一个重要的区别,如果你是使用不同的全局 node.js 版本,例如 NVM.

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.

npm install/absolute/path/x 将改变 package.json,npm link x没有.

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

要获取新的本地副本而不是符号链接,请使用 npm pack,如下所示:

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"

您也可以使用 cp/rsync,但这不会运行安装挂钩或将可执行文件放在 node_modules/.bin 中...那会起作用.

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