IT박스

NPM 모듈은 sudo없이 전체적으로 설치되지 않습니다

itboxs 2020. 7. 13. 21:43
반응형

NPM 모듈은 sudo없이 전체적으로 설치되지 않습니다


방금 Ubuntu 12.04 LTS를 다시 설치했으며 다른 작업 전에 다음 단계를 수행했습니다 .

  1. 다음 스크립트를 사용하여 패키지 관리자를 통해 설치된 노드

    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. yeoman, express, n, yeoman의 발전기를 전세계에 설치하려고 시도했지만 모두 동일한 오류를 반환했습니다.

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

    npm ERR! {[오류 : EACCES, symlink '../lib/node_modules/n/bin/n'] 오류 : 3, 코드 : 'EACCES', 경로 : '../lib/node_modules/n/bin/n'}

    npm ERR!

    npm ERR! 루트 / 관리자로이 명령을 다시 실행하십시오.

    npm ERR! 시스템 리눅스 3.8.0-29-generic

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

    npm ERR! cwd / home / heberlz

    npm ERR! 노드 -v v0.10.20

    npm ERR! npm -v 1.3.11

    npm ERR! 경로 ../lib/node_modules/n/bin/n

    npm ERR! 코드 EACCES

    npm ERR! 에르 노 3

    npm ERR! 스택 오류 : EACCES, symlink '../lib/node_modules/n/bin/n'

    npm ERR!

    npm ERR! 추가 로깅 세부 사항은 다음에서 찾을 수 있습니다.

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

    npm ERR! 유효하지 않은 코드 0

  3. 다음 폴더의 매립지 소유권 반복적으로 ~ / .npm 인 / usr / lib 디렉토리 / 노드는 / usr / lib 디렉토리 / node_modules, 다음과 같은 심볼릭 링크는 / usr / 빈 / 노드의 인 / usr / 빈은 / nodejs 전혀 성공

나중에 문제가 발생하지 않도록 yeoman과 발전기를 sudo없이 설치해야합니다.


Ubuntu 12.04에서 Chris Lea의 PPA를 사용하여 다음을 설치하십시오.

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

$ HOME / .npm-packages / bin을 $ PATH에 추가

덧붙이다 .bashrc

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

@passy의 https://stackoverflow.com/a/18277225 참조


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

npm config set prefix ~
  • New node commands will now install into your $HOME/bin directory.
  • 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.

In general, it's safest to:

  • (a) Place $HOME/bin last in your path so system commands are not superseded.
  • (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.

Reference:


As for October 2014:

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

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

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.

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:

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.


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


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.


Find the path to npm's directory:

npm config get prefix

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

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

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).

Here is the link for full details

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


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

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


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!


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


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.

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


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

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

This will install Nodejs under ~/.local/ instead of the default /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

chown -R user:user ~/.local/

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

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

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

참고URL : https://stackoverflow.com/questions/19352976/npm-modules-wont-install-globally-without-sudo

반응형