沒有sudo, NPM模塊不會在全球安裝。

[英]NPM modules won't install globally without sudo


I have just reinstalled Ubuntu 12.04 LTS, and before anything else i did these steps:

我剛剛重新安裝了Ubuntu 12.04 LTS,在我做這些步驟之前:

  1. Installed Node via package manager with the following script

    通過包管理器安裝節點,使用以下腳本。

    sudo apt-get update
    
    sudo apt-get install python-software-properties python g++ make
    
    sudo add-apt-repository ppa:chris-lea/node.js
    
    sudo apt-get update
    
    sudo apt-get install nodejs
    
  2. Tried to install yeoman, express, n, yeoman's generators globally and all of them returned the same error

    試圖在全球范圍內安裝yeoman、express、n、yeoman的生成器,所有這些生成器都返回了相同的錯誤。

    npm ERR! Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm犯錯!錯誤:EACCES,符號鏈接“. . / lib / node_modules / n / bin / n '

    npm ERR! { [Error: EACCES, symlink '../lib/node_modules/n/bin/n'] errno: 3, code: 'EACCES', path: '../lib/node_modules/n/bin/n' }

    npm犯錯!{[錯誤:EACCES, symlink '../lib/node_modules/n/bin/n' errno: 3,代碼:'EACCES', path: '../lib/node_modules / n / bin / n ' }

    npm ERR!

    npm犯錯!

    npm ERR! Please try running this command again as root/Administrator.

    npm犯錯!請嘗試再次運行此命令作為根/管理員。

    npm ERR! System Linux 3.8.0-29-generic

    npm犯錯!Linux系統3.8.0-29-generic

    npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "-d" "n"

    npm犯錯!命令/ usr / bin /節點”“/ usr / bin / npm”“安裝”“g”“- d”“n”

    npm ERR! cwd /home/heberlz

    npm犯錯!/home/heberlz慢性消耗性疾病

    npm ERR! node -v v0.10.20

    npm犯錯!節點- v v0.10.20

    npm ERR! npm -v 1.3.11

    npm犯錯!npm - v 1.3.11

    npm ERR! path ../lib/node_modules/n/bin/n

    npm犯錯!路徑. . / lib / node_modules / n / bin / n

    npm ERR! code EACCES

    npm犯錯!代碼EACCES

    npm ERR! errno 3

    npm犯錯!errno 3

    npm ERR! stack Error: EACCES, symlink '../lib/node_modules/n/bin/n'

    npm犯錯!堆棧錯誤:EACCES, symlink '. /lib/node_modules/n/bin/n'

    npm ERR!

    npm犯錯!

    npm ERR! Additional logging details can be found in:

    npm犯錯!額外的日志記錄細節可以在:

    npm ERR! /home/heberlz/npm-debug.log

    npm犯錯!/home/heberlz/npm-debug.log

    npm ERR! not ok code 0

    npm犯錯!不是好的代碼0

  3. Reclaimed ownership of the following folders recursively ~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success

    以下文件夾的回收所有權遞歸~/。npm, /usr/lib/node, /usr/lib/node_modules,以及下面的符號鏈接/usr/bin/node, /usr/bin/nodejs,完全沒有成功。

I need to install yeoman and its generators without sudo not to be in trouble later on :(

我需要安裝yeoman和它的發電機,而不需要sudo在以后遇到麻煩。

13 个解决方案

#1


300  

Ubuntu 12.04 and using Chris Lea's PPA for install the following works for me:

Ubuntu 12.04使用Chris Lea的PPA為我安裝以下工作:

npm config set prefix '~/.npm-packages'

and adding $HOME/.npm-packages/bin to $PATH

並添加$ HOME /。npm-packages美元/ bin路徑

append to .bashrc

添加到. bashrc

export PATH="$PATH:$HOME/.npm-packages/bin"

see https://stackoverflow.com/a/18277225 from @passy

看到https://stackoverflow.com/a/18277225 @passy

#2


19  

If you already have $HOME/bin in your path, a simpler solution is just ...

如果您已經在您的路徑中有$HOME/bin,那么更簡單的解決方案就是……

npm config set prefix ~
  • New node commands will now install into your $HOME/bin directory.
  • 新的節點命令將安裝到$HOME/bin目錄中。
  • No need to change your path!
  • 不需要改變你的道路!

Since this discussion is really about reducing the security risks of running sudo, you should also be aware that any node app could potentially be installing an app name that does not match the registered node package name you think you're installing. So there is a security risk that an npm install will replace an existing system command or one you already have in $HOME/bin. If you're concerned, check the bin, and scripts properties in the package.json file of the app you're installing first.

由於這個討論實際上是關於減少運行sudo的安全風險,您還應該知道,任何節點應用程序都可能安裝一個與您認為正在安裝的注冊節點包名稱不匹配的應用程序名稱。因此,npm安裝將會取代現有的系統命令或您已經擁有的$HOME/bin的安全風險。如果您感興趣,請檢查包中的bin和腳本屬性。您正在安裝的應用程序的json文件。

In general, it's safest to:

一般來說,這是最安全的:

  • (a) Place $HOME/bin last in your path so system commands are not superseded.
  • (a)將$HOME/bin放在您的路徑中,這樣系統命令就不會被取代。
  • (b) don't include "." or any relative path in your $PATH so you don't accidentally run a command that happens to be in the current directory.
  • (b)不包括“。”或您的$ path中的任何相對路徑,這樣您就不會意外地運行在當前目錄下的命令。

Reference:

參考:

#3


13  

As for October 2014:

至於2014年10月:

Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository.

節點。js可以從NodeSource Debian和Ubuntu二進制發行版本存儲庫中獲得。

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

That's it.

就是這樣。

Outdated answer:

過時的回答:

The fastest way without using sudo is like described here by isaac

不使用sudo的最快方法是用isaac描述的。

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it's fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

我強烈建議您不要用sudo做包管理!包可以運行任意的腳本,這使得sudo包管理器的命令和鏈鋸一樣安全。當然,它是快速的,肯定會突破任何障礙,但你可能真的想要那個障礙留在那里。

I recommend doing this once instead:

我建議這樣做一次:

sudo chown -R $USER /usr/local

EDIT:

編輯:

There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:

更改/usr/local對當前用戶的所有權有一定的安全考慮和功能限制:

Having said that, if you want to install global module without using sudo, I don't see any better solution (from pragmatic point of view) than mentioned. Security vs easy of use is very broad topic, and there is no easy answer for that - it just depends on your requirements.

已經說過,如果您想要在不使用sudo的情況下安裝全局模塊,那么我沒有看到任何更好的解決方案(從實用的角度來看)。安全性vs易於使用是一個非常寬泛的主題,並且沒有簡單的答案——它僅僅取決於您的需求。

#4


10  

The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with sudo.

問題是我使用sudo安裝了node,為了避免在全局安裝npm模塊時出現錯誤,一個節點不能安裝sudo節點。

My solution was to reinstall node it this way:

我的解決方案是這樣重新安裝節點:

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

從nodejs.org #中下載最新的穩定節點源,在我的案例中,node-v0.10.20.tar.gz。

tar -zxf node-v0.10.20.tar.gz #uncompress sources

焦油-zxf node-v0.10.20.tar。廣州#解壓源

cd node-v0.10.20 #enter uncompressed folder

cd node-v0.10.20 #進入未壓縮文件夾。

sudo chown -R $USER /usr/local

sudo chown -R $USER /usr/local。

./configure --prefix=/usr/local && make && make install

./configure -prefix=/usr/local && make && make安裝。

One thing to note is that only taking ownership of the /usr/local folder wouldn't work in my case because node installation itself was made with sudo

需要注意的一點是,只有使用/usr/local文件夾的所有權在我的情況下不會起作用,因為節點安裝本身是由sudo進行的。

Last step to install yeoman: #although at yeoman.io it says that doing "npm install -g yo" already installs bower and grunt, there are some submodules of grunt that fail, so i fixed that by installing it by itself

安裝yeoman的最后一步:#盡管在yeoman。它說,“npm安裝-g yo”已經安裝了bower和grunt,有一些grunt的子模塊失敗了,所以我自己安裝了它。

npm install -g bower

npm安裝- g鮑爾

npm install -g grunt

npm安裝- g繁重

npm install -g yo

npm安裝- g喲

npm install -g generator-angular

npm安裝- g generator-angular

#5


4  

I solved this problem with environment variable and shell alias:

我用環境變量和shell別名解決了這個問題:

export NPM_PREFIX=$HOME/node
alias npmg="npm -g --prefix $NPM_PREFIX"

For me npm did not honor the "prefix" config setting in .npmrc.

對於我來說,npm並沒有遵守.npmrc中的“前綴”配置設置。

#6


2  

According to this similar SO post: npm throws error without sudo

根據這個相似的帖子:npm拋出錯誤而沒有sudo。

Looks like you might have an ownership issue with ~/.npm directory.

看起來你可能對~/有所有權問題。npm目錄。

As with the answer in that one, try:

就像這個問題的答案一樣,試試:

sudo chown -R `whoami` ~/.npm

#7


1  

Actually, I just changed the permission of a user folder that was owned by root:

實際上,我只是修改了root用戶文件夾的權限:

sudo chown -R $USER ~/.config/configstore

Then I could "npm install" and "bower install" without sudo! Worked fine!

然后我可以“npm安裝”和“bower安裝”沒有sudo!工作得很好!

#8


0  

If you're on a developping machine, you might be better off considering using nvm.

如果您是在開發機器上,您最好考慮使用nvm。

If not, you simply want to install using your favorite package manager.

如果沒有,您只需使用您最喜歡的包管理器安裝。

Whatever the case may be, I'd recommend checking this answer on stackoverflow

無論情況如何,我建議在stackoverflow上檢查這個答案。

#9


0  

using lubuntu 14.04.3, I tried changing ownership of .npm and npm prefix, updated my path, npm installed modules to my home directory without sudo but the path was incorrect so the modules like ember were not found, linuxbew solved the problem, quick setup guide here for node/npm

使用lubuntu 14.04.3,我嘗試更改了.npm和npm前綴的所有者,更新了我的路徑,npm將模塊安裝到我的主目錄,沒有sudo,但是路徑是不正確的,所以沒有找到像ember這樣的模塊,linuxbew解決了這個問題,在這里快速設置了node/npm的安裝指南。

#10


0  

This issue and other caused by the same reason can be solved installing Node in user space.

這個問題和其他由相同原因引起的問題可以在用戶空間中解決安裝節點。

You can do it just copying and pasting in your terminal

你可以在終端復制粘貼。

NODEJS_ROOT=${NODEJS_ROOT:-~/nodejs}
cd /tmp
wget -N http://nodejs.org/dist/node-latest.tar.gz && tar xzf node-latest.tar.gz
NODEJS_CURRENT=$(tar tf node-latest.tar.gz|head -1)
mkdir -p $NODEJS_ROOT/$NODEJS_CURRENT
cd $NODEJS_CURRENT
./configure --prefix=$NODEJS_ROOT/$NODEJS_CURRENT && make install
cd $NODEJS_ROOT
rm current 2> /dev/null # Removes current symbolic link, if any
ln -s $NODEJS_CURRENT current

Same commands can be launched also to get Node updated to latest version.

同樣的命令也可以啟動,讓節點更新到最新版本。

Don't forget to edit your environment. Only once, do

別忘了編輯你的環境。只有一次,

echo "export NODEJS_ROOT=$NODEJS_ROOT"            >> $HOME/.bash_profile
echo 'export PATH=$NODEJS_ROOT/current/bin:$PATH' >> $HOME/.bash_profile
source $HOME/.bash_profile # reload your env, so you can use node right now

Check out this article as a reabout how to Install Node.js without sudo.

請查看這篇文章,討論如何安裝節點。js沒有sudo。

For a more general solution about this topic (i.e., install software locally) see dotsoftware.

關於這個主題的更一般的解決方案(例如,在本地安裝軟件)見dotsoftware。

#11


0  

Find the path to npm's directory:

找到npm目錄的路徑:

npm config get prefix

For many systems, this will be /usr/local.

對於許多系統,這將是/usr/ local。

Change the owner of npm's directories to the name of the current user (your username!):

將npm目錄的所有者更改為當前用戶的名稱(您的用戶名!):

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

這將更改npm和其他一些工具(lib/node_modules、bin和share)使用的子文件夾的權限。

Here is the link for full details

這里是詳細信息的鏈接。

https://docs.npmjs.com/getting-started/fixing-npm-permissions

https://docs.npmjs.com/getting-started/fixing-npm-permissions

#12


-1  

I find Pawel Grzybek's explanations very convincing: They boil down to 3 simple sudo commands, never having to use sudo again for global npm installs:

我發現Pawel Grzybek的解釋非常有說服力:它們可以歸結為3個簡單的sudo命令,再也不用在全球npm安裝上使用sudo了:

sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share

#13


-2  

The best solution I found was to install Node.js from the tar package on to user home directory & link the lib folder location. Here is what you need to do

我找到的最佳解決方案是安裝Node。從tar包到用戶主目錄&鏈接lib文件夾位置。這是你需要做的。

This will install Nodejs under ~/.local/ instead of the default /usr/local/

這將在~/下安裝Nodejs。本地/而不是默認/usr/local/。

Add this to your ~/.npmrc (create the file if it doesn't exist already):

root =    /home/YOUR-USERNAME/.local/lib/node_modules
binroot = /home/YOUR-USERNAME/.local/bin
manroot = /home/YOUR-USERNAME/.local/share/man
Download the Nodejs source code from nodejs.org and install it under your ~/.local tree:

tar xf node......
cd node........
./configure --prefix=~/.local
make
make install

Create ~/.node_modules symlink. (This directory will be automatically searched when you load modules using require "module" in scripts. I'm not sure why Node doesn't search ~/.local/lib/node_modules by default.)

cd
ln -s .local/lib/node_modules .node_modules
Is ~/.local/bin in your path? Type

which npm
If it says ~/.local/bin/npm, you're done.

Otherwise, do this...

export PATH=$HOME/.local/bin:$PATH
...and add that line to your ~/.profile file, so it'll run every time you log in.

If you still encounter ownership or permission error while installing packages, then change ownership of ~/.local/ dir by running

如果在安裝包時仍然遇到所有權或權限錯誤,則更改~/的所有權。本地/ dir通過運行

chown -R user:user ~/.local/

Now you should be good to install packages via 'npm'

現在您應該可以通過'npm'安裝包了

Note: ALL OF THE ABOVE COMMANDS ARE TO BE RUN AS USER. DO NOT USE SUDO OR ROOT LOGIN

注意:以上所有命令都將作為用戶運行。不使用SUDO或ROOT登錄?

NEVER EVER CHANGE THE PERMISSION OF FOLDERS UNDER '/USR/LIB/'. WILL LEAD TO UNSTABLE OS

永遠不要更改“/USR/LIB/”下文件夾的權限。會導致不穩定的操作系統嗎?


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/14/7255eb99b969fed5b9988dc8783400f9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com