使用分发 nodejs 包 (Ubuntu) 将 NPM 安装到主目录中 [英] Install NPM into home directory with distribution nodejs package (Ubuntu)

查看:24
本文介绍了使用分发 nodejs 包 (Ubuntu) 将 NPM 安装到主目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用分发版 Node.js 包(或 chris-lea ppa 用于更新的版本)但将 NPM 安装到我的主目录.

I'd like to use the distribution Node.js packages (or the chris-lea ppa for more recent releases) but install NPM to my home directory.

这可能看起来很挑剔,但对于使用多语言/github 的开发人员来说,这是在 Linux 下设置语言运行时/库环境的一种非常惯用的方式:运行时的发行版包,每个用户环境中的 3rd 方库(请参阅 virtualenv,RVM - 如果您愿意,RVM 还将为您构建 Ruby).如有必要,我会在本地构建节点,但它是一个 PITA,因为 Node 正在成为许多项目的附带开发需求.

This may seem picky, but it's a pretty idiomatic way for polyglot/github-using developers to setup language runtime/library environments under Linux: distro packages for the runtime, 3rd-party libraries in per-user environment (see virtualenv, RVM - RVM will also build Ruby for you if you want). If necessary I will build node locally but it's a PITA since Node is becoming an incidental development requirement for lots of projects.

推荐答案

NPM 已经将本地包安装到您的项目中,但我仍然希望让系统远离我的操作系统文件.以下是我建议划分 Nodejs 包的方法:

NPM will install local packages into your projects already, but I still like to keep the system away from my operating system's files. Here's how I suggest compartmentalizing Nodejs packages:

通过 chris-lea PPA 安装 Nodejs 和 NPM.然后我在我的 homedir 中设置了一个包根目录来保存节点全局".包:

Install Nodejs and NPM via the chris-lea PPA. Then I set up a package root in my homedir to hold the Node "global" packages:

 $ NPM_PACKAGES="$HOME/.npm-packages"
 $ mkdir -p "$NPM_PACKAGES"

设置 NPM 使用此目录进行全局包安装:

Set NPM to use this directory for its global package installs:

 $ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc

通过将以下内容添加到 .zshrc/.bashrc 中,配置 PATH 和 MANPATH 以查看 $NPM_PACKAGES 前缀中的命令:

Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .zshrc/.bashrc:

# 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/lib/node_modulescode>~/.npm-packages/bin,在你的PATH中.

Now when you do an npm install -g, NPM will install the libraries into ~/.npm-packages/lib/node_modules, and link executable tools into ~/.npm-packages/bin, which is in your PATH.

只需像往常一样使用 npm install -g:

Just use npm install -g as you would normally:

[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

这篇关于使用分发 nodejs 包 (Ubuntu) 将 NPM 安装到主目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆