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

查看:308
本文介绍了使用分发节点js软件包(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下设置语言运行时/库环境:运行时的发行版包,每个用户环境中的第三方库(请参阅virtualenv, RVM-如果需要,RVM还将为您构建Ruby).如有必要,我将在本地构建节点,但这是一个PITA,因为节点已成为许多项目的附带开发要求.

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中设置了一个包根目录,以保存Node全局"包:

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中,并将可执行工具链接到PATH中的~/.npm-packages/bin中.

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:

[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

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

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