使用分发 nodejs 包 (Ubuntu) 将 NPM 安装到主目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10081293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Install NPM into home directory with distribution nodejs package (Ubuntu)
提问by Eric Drechsel
I'd like to use the distribution Node.js packages (or the chris-lea ppafor more recent releases) but install NPM to my home directory.
我想使用分发的 Node.js 包(或chris-lea ppa用于更新的版本),但将 NPM 安装到我的主目录。
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.
这可能看起来很挑剔,但对于使用多语言/github 的开发人员来说,这是在 Linux 下设置语言运行时/库环境的一种非常惯用的方式:运行时的发行版包,每个用户环境中的第 3 方库(请参阅 virtualenv、RVM - RVM如果您愿意,还将为您构建 Ruby)。如有必要,我将在本地构建节点,但它是一个 PITA,因为 Node 正在成为许多项目的附带开发需求。
回答by Just Jake
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:
NPM 已经将本地包安装到您的项目中,但我仍然希望让系统远离我的操作系统文件。以下是我建议划分 Nodejs 包的方法:
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:
通过 chris-lea PPA 安装 Nodejs 和 NPM。然后我在 homedir 中设置了一个包根目录来保存 Node 的“全局”包:
$ export NPM_PACKAGES="$HOME/.npm-packages"
$ mkdir -p "$NPM_PACKAGES"
Set NPM to use this directory for its global package installs:
设置 NPM 使用这个目录来安装它的全局包:
$ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc
Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .zshrc/.bashrc:
通过将以下内容添加到 .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"
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,NPM将安装到库~/.npm-packages/lib/node_modules,并链接可执行工具进入~/.npm-packages/bin,这是你的PATH。
Just use npm install -gas you would normally:
只需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
[email protected] /home/justjake/.npm-packages/lib/node_modules/coffee-script
[justjake@marathon:~] $ which coffee
/home/justjake/.npm-packages/bin/coffee
回答by Maxime R.
Jake's answer was posted in 2012 and while useful it references Chris Lea's Node.js PPAs who are no longer updated since march 2015.
Jake 的回答发布于 2012 年,虽然有用,但它引用了自 2015 年 3 月以来不再更新的 Chris Lea 的 Node.js PPA。
Here's the steps I use to install Node.js and npm in my home directory:
下面是我用来在我的主目录中安装 Node.js 和 npm 的步骤:
Install Node.js with nvm(no sudorequired):
使用nvm安装 Node.js (sudo不需要):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
nvm install 7
npm install -g npm # update npm
Now you can install -gwithout sudoand everything goes into ~/.nvm/
现在你可以install -g不用sudo,一切都会进入~/.nvm/
Or install Node.js without nvm (official instructions):
或者在没有 nvm 的情况下安装 Node.js(官方说明):
Install Node.js
安装 Node.js
Node.js v6 (current LTS as of May 2017):
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejsNode.js v7:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - sudo apt-get install -y nodejs
Node.js v6(截至 2017 年 5 月的当前 LTS):
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejsNode.js v7:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - sudo apt-get install -y nodejs
Change npm's default directory to a local one:
将 npm 的默认目录更改为本地目录:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH" # ← put this line in .bashrc
source ~/.bashrc # if you only updated .bashrc
Alternatively replace .npm-globalby the directory of your choice.
或者替换.npm-global为您选择的目录。
Update npm and check it is installed in your $HOMEdirectory:
更新 npm 并检查它是否安装在您的$HOME目录中:
$ npm install npm -g
/home/<username>/.npm-global/bin/npm -> /home/<username>/.npm-global/lib/node_modules/npm/bin/npm-cli.js
/home/<username>/.npm-global/lib
└─┬ [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
└── [email protected]
Now you can install -gwithout sudoand without messing with your system files.
现在您可以install -g不用sudo也不会弄乱您的系统文件。
回答by user1533401
The solution posted by Just Jake is great. However, due to a bug with npm > 1.4.10, it may not work as expected. (See thisand this)
Just Jake 发布的解决方案很棒。但是,由于 npm > 1.4.10 的错误,它可能无法按预期工作。(见这个和这个)
While the bug is solved, you can downgrade to npm 1.4.10 by following this steps:
问题解决后,您可以按照以下步骤降级到 npm 1.4.10:
- Comment the prefix line in your $HOME/.npmrc
- Run
sudo npm install -g [email protected] - Ensure that the right version of npm is installed (
npm --version) - Uncomment the prefix line in your $HOME/.npmrc
- Proceed to install your global packages in your home folder!.
- 注释 $HOME/.npmrc 中的前缀行
- 跑
sudo npm install -g [email protected] - 确保安装了正确版本的 npm (
npm --version) - 取消注释 $HOME/.npmrc 中的前缀行
- 继续在您的主文件夹中安装您的全局包!。
回答by dnozay
Because python does already a great job virtualenv, I use nodeenv. Compared to nvm, you can create multiple environments for the same node version (e.g. two environments for node 0.10but with different sets of packages).
因为 python 已经做得很好virtualenv,所以我使用nodeenv. 与 相比nvm,您可以为同一节点版本创建多个环境(例如,节点的两个环境0.10但具有不同的包集)。
ENVNAME=dev1
# create an environment
python -m virtualenv ${ENVNAME}
# switch to the newly created env
source ${ENVNAME}/bin/activate
# install nodeenv
pip install nodeenv
# install system's node into virtualenv
nodeenv --node=system --python-virtualenv
The readme is pretty good: https://github.com/ekalinin/nodeenv
自述文件非常好:https: //github.com/ekalinin/nodeenv
回答by rutsky
I used @just-jake solutionfor some time and found that nvmis easier to setup.
Also it's much powerful solution that allows to install and use different versions of nodejs.
我使用@just-jake解决方案有一段时间了,发现nvm更容易设置。它也是一个非常强大的解决方案,允许安装和使用不同版本的nodejs.
On Ubuntu 14.04 or 16.04:
在 Ubuntu 14.04 或 16.04 上:
Install prerequisite packages for building
nodejs:sudo apt-get update sudo apt-get install build-essential libssl-devInstall nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bashIn case newer version of nvm will be available you can find actual installation command on nvmsite.
nvminstaller will add bootstrap script to~/.bashrc, so you need either to reopen terminal to run it, or to do:source ~/.bashrcNow you can install any
nodejsversion you like, switch between them etc.Use
nvm ls-remoteto list availablenodejsversions.To install, for example,
nodejsv4.2.4 do:# install v4.2.4 nvm install v4.2.4 # use nodejs v4.2.4 in the current terminal session nvm use v4.2.4 # use v4.2.4 by default in new terminal session nvm alias default v4.2.4
安装用于构建的必备软件包
nodejs:sudo apt-get update sudo apt-get install build-essential libssl-dev安装nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash如果有新版本的 nvm 可用,您可以在nvm站点上找到实际的安装命令。
nvm安装程序会将引导脚本添加到~/.bashrc,因此您需要重新打开终端才能运行它,或者执行以下操作:source ~/.bashrc现在你可以安装任何
nodejs你喜欢的版本,在它们之间切换等等。使用
nvm ls-remote列出可用的nodejs版本。例如,要安装
nodejsv4.2.4,请执行以下操作:# install v4.2.4 nvm install v4.2.4 # use nodejs v4.2.4 in the current terminal session nvm use v4.2.4 # use v4.2.4 by default in new terminal session nvm alias default v4.2.4
回答by rofrol
回答by Bart Louwers
To expand on the answer provided by Just Jake and user1533401: I am unable to downgrade as I use shared hosting and node is installed in a system directory. This is also why I have change the directory where npm installs global scripts if I want it to do that. For those in the same boat, here is a another temporary fix I found works:
扩展 Just Jake 和 user1533401 提供的答案:我无法降级,因为我使用共享主机并且节点安装在系统目录中。这也是为什么我要更改 npm 安装全局脚本的目录的原因。对于那些在同一条船上的人,这是我发现的另一个临时修复方法:
npm install -g --prefix=$(npm config get prefix) <package>
npm install -g --prefix=$(npm config get prefix) <package>
The bug is that npm doesn't read your per-user config file, but specifying it every time you install a global script fixes that. Found here.
错误在于 npm 不会读取您的每用户配置文件,但每次安装全局脚本时都指定它可以修复该问题。在这里找到。

