NPM 已经将本地包安装到您的项目中,但我仍然希望系统远离我的操作系统文件。以下是我建议划分 Nodejs 包的方法:
通过 chris-lea PPA 安装 Nodejs 和 NPM。然后我在我的 homedir 中设置了一个包根来保存节点“全局”包:
$ NPM_PACKAGES="$HOME/.npm-packages"
$ mkdir -p "$NPM_PACKAGES"
设置 NPM 使用这个目录来安装它的全局包:
$ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc
通过将以下内容添加到 .zshrc/.bashrc 中,配置 PATH 和 MANPATH 以查看 $NPM_PACKAGES 前缀中的命令:
# NPM packages in homedir
NPM_PACKAGES="$HOME/.npm-packages"
# Tell our environment about user-installed node tools
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your configuration
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
# Tell Node about these packages
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
现在,当您执行. 时,npm install -g
NPM 会将库安装~/.npm-packages/lib/node_modules
到.~/.npm-packages/bin
PATH
npm install -g
像往常一样使用:
[justjake@marathon:~] $ npm install -g coffee-script
... (npm downloads stuff) ...
/home/justjake/.npm-packages/bin/coffee -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/coffee
/home/justjake/.npm-packages/bin/cake -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/cake
coffee-script@1.3.3 /home/justjake/.npm-packages/lib/node_modules/coffee-script
[justjake@marathon:~] $ which coffee
/home/justjake/.npm-packages/bin/coffee