npm link 命令解析

 文字转的node

对开发者而言,这算是最有价值的命令。假设咱们开发了一个模块叫 test ,而后咱们在 test-example 里引用这个模块 ,每次 test 模块的变更咱们都须要反映到 test-example 模块里。不要担忧,有了 npm link 命令一切变的很是容易。linux

首先咱们须要把 test 连接到全局模式下:npm

cd ~/work/node/test # 进入test模块目录
npm link # 建立连接到$PREFIX/lib/node_modules

那么 test 的模块将被连接到 $PREFIX/lib/node_modules 下,就像个人机器上 $PREFIX 指到 /usr/local ,那么 /usr/local/lib/node_modules/test 将会连接到 ~/work/node/test 下。执行脚本 bin/test.js 被连接到 /usr/local/bin/test 上。spa

接下来咱们须要把 test 引用到 test-example 项目中来:3d

cd ~/work/node/test-example # 进入test-example模块目录
npm link test # 把全局模式的模块连接到本地

npm link test 命令会去 $PREFIX/lib/node_modules 目录下查找名叫 test 的模块,找到这个模块后把 $PREFIX/lib/node_modules/test 的目录连接到 ~/work/node/test-example/node_modules/test 这个目录上来。blog

如今任何 test 模块上的改动都会直接映射到 test-example 上来。ip

 

再好比假设咱们开发不少应用,每一个应用都用到 Coffee-script :开发

npm install coffee-script -g # 全局模式下安装coffee-script
cd ~/work/node/test # 进入开发目录
npm link coffee-script # 把全局模式的coffee-script模块连接到本地的node_modules下
cd ../test-example # 进入另外的一个开发目录
npm link coffee-script # 把全局模式的coffee-script模块连接到本地
npm update coffee-script -g # 更新全局模式的coffee-script,全部link过去的项目同时更新了。

 

 

 

原理  linux的系统下会自动执行ln -s 命令来建立一个软链接指向你的全局包路径test

 

这么必定段文字有点绕。下面用画图解释一下 module

 

例:

test-example 使用须要 test模块,就是上文红色文字

 

 

例2 :每一个应用都用到 Coffee-script 上文蓝色字

 

相关文章
相关标签/搜索