简体   繁体   中英

Unable to install global modules using sudo npm

I was trying to install pm2, sails.js for Node.js from a non-root user with sudo. I get the below error:

sudo npm install pm2 -g
sudo: npm: command not found

but when i try to install using the below command, it works

sudo /usr/local/bin/npm install pm2 -g

How can i make sudo npm work?

Thanks.

How i installed Node and npm with su previously:

su - 
yum install gcc-c++ openssl-devel python
cd /usr/local/src
wget http://nodejs.org/dist/node-latest.tar.gz
tar zxvf node-latest.tar.gz
(cd into extracted folder: ex "cd node-v0.10.3")
./configure
make
make install

It means the npm is not install with sudo, so it is not in the path of root. I think, it is better to always use npm without 'sudo'. Because you can keep the packaged installed with npm in your local environment.

If you really want to use with sudo, you can re-install node with sudo.

Yes, NPM is not installed on your system. you can check first if node is set perfectly.

$node -v

it will give version. if yes then check

$npm -v

if its not giving version may be your environment path is not set then execute

$PATH=/usr/bin/node:$PATH

and just check node -v and npm -v . if npm gives version number. thats it. It will install global package.

A big thanks to everyone who helped me out! Just like @Rodrigo Medeiros advised me, i installed node.js and npm using the below method taken from ( https://gist.github.com/isaacs/579814#file-take-ownership-sh ) but modified two lines of commands as per the advise from a comment posted by deesejohn in that page.

cd
sudo yum install gcc-c++
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=$HOME/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl -L https://www.npmjs.org/install.sh | sh

Check installed version using node -v and npm -v

Now, i am able to install npm modules globally without sudo permissions, there are many ways to install node.js and npm but this worked for me.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM