NPM modules won't install globally without sud

2019-01-01 17:10发布

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

  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

    npm ERR! Error: EACCES, symlink '../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 ERR!

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

    npm ERR! System Linux 3.8.0-29-generic

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

    npm ERR! cwd /home/heberlz

    npm ERR! node -v v0.10.20

    npm ERR! npm -v 1.3.11

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

    npm ERR! code EACCES

    npm ERR! errno 3

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

    npm ERR!

    npm ERR! Additional logging details can be found in:

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

    npm ERR! not ok code 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

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

13条回答
春风洒进眼中
2楼-- · 2019-01-01 17:16

If you're on a developping machine, you might be better off considering using 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

查看更多
有味是清欢
3楼-- · 2019-01-01 17:17

The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with 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

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

cd node-v0.10.20 #enter uncompressed folder

sudo chown -R $USER /usr/local

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

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

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

npm install -g bower

npm install -g grunt

npm install -g yo

npm install -g generator-angular

查看更多
栀子花@的思念
4楼-- · 2019-01-01 17:21

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

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

As with the answer in that one, try:

sudo chown -R `whoami` ~/.npm
查看更多
长期被迫恋爱
5楼-- · 2019-01-01 17:22

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:

sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share
查看更多
何处买醉
6楼-- · 2019-01-01 17:23

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

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

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

查看更多
梦寄多情
7楼-- · 2019-01-01 17:27

I solved this problem with environment variable and shell alias:

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

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

查看更多
登录 后发表回答