越努力,越幸运,做个ccode~

0%

npm link详解

Package linking is a two-step process.

  • npm link
  • npm link package-name
  • 首先 npm link 会在全局目录{prefix}/lib/node_modules/<package>
    创建一个当前目录的软连接
  • 将包中bin下的命令 链接到{prefix}/bin/{name} 目录
  • npm config get prefix 可以查看prefix
1
// package.json
2
"bin":{
3
  "test": "./bin/test.js",
4
  "test2": "./bin/test2.js"
5
}
  • 另一个目录 npm link package-name 将创建一个从全局安装的包名到当前文件夹的node_modules/的符号链接。
  • 注意 package-name 是从package.json 中的名字 不是目录名

The package name can be optionally prefixed with a scope. See npm-scope. The scope must be preceded by an @-symbol and followed by a slash.
包名称可以选择性地加上作用域前缀。参见npm-scope。作用域前面必须有@-符号,后面必须有斜杠。

When creating tarballs for npm publish, the linked packages are “snapshotted” to their current state by resolving the symbolic links.
通过npm publish 命令去发布 链接的包将“快照”到其当前状态。

This is handy for installing your own stuff, so that you can work on it and test it iteratively without having to continually rebuild.
这对于安装您自己的东西很方便,因此您可以对其进行操作并进行迭代测试,而无需不断地重新构建。

For example:

1
cd ~/projects/node-redis    # go into the package directory
2
npm link                    # creates global link
3
cd ~/projects/node-bloggy   # go into some other package directory.
4
npm link redis              # link-install the package

Now, any changes to ~/projects/node-redis will be reflected in ~/projects/node-bloggy/node_modules/node-redis/. Note that the link should be to the package name, not the directory name for that package.

You may also shortcut the two steps in one. For example, to do the above use-case in a shorter way:
cd ~/projects/node-bloggy # go into the dir of your main project
npm link ../node-redis # link the dir of your dependency

The second line is the equivalent of doing:

1
(cd ../node-redis; npm link)
2
npm link redis

That is, it first creates a global link, and then links the global installation target into your project’s node_modules folder.

Note that in this case, you are referring to the directory name, node-redis, rather than the package name redis.
If your linked package is scoped (see npm-scope) your link command must include that scope, e.g.
npm link @myorg/privatepackage