Coder Perfect

npm: command not found in sudo

Problem

I’m attempting to upgrade node to the most recent version. I’m following http://davidwalsh.name/upgrade-nodejs/’s instructions.

However, when I do:

sudo npm install -g n

I’m getting the following error:

sudo: npm: command not found

npm works without sudo. When I do this:

whereis node

I see:

node: /usr/bin/node /usr/lib/node /usr/bin/X11/node /usr/local/node

Running:

which npm

Shows:

/usr/local/node/bin/npm

I attempted to solve the problem at https://stackoverflow.com/a/5062718/1246159.

However, I continue to receive the same error. I also checked the /etc/sudoers file and found the following line:

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

This appears to be in good working order to me. How do I get NPM to operate with the sudo command?

Asked by Mark

Solution #1

I had to do

sudo apt-get install npm

That was effective for me.

Answered by Velocibadgery

Solution #2

/usr/local/bin/npm should contain the npm file. If it isn’t there, get the package from their website and reinstall node.js. In my case, it worked.

Answered by inga

Solution #3

The methods below worked for me as a MAC user.

If you get an Error for Brew, type the following command in the terminal first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
$ brew update
$ brew uninstall node
$ brew install node
$ brew postinstall 

Answered by Gao

Solution #4

I had the same problem; here are the commands to fix it:

Answered by Vasanth Umapathy

Solution #5

WARNING: Performing a chmod 777 is a drastic solution. Try these first, one at a time, and stop when one works:

The only step of the installation where I’d have a problem is $ brew postinstall node.

Permission denied - /usr/local/lib/node_modules/npm/.github

So I

// !! READ EDIT ABOVE BEFORE RUNNING THIS CODE !!
$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node

and there you have it, npm is now linked.

$ npm -v
3.10.10

Extra

If you used -R 777 on lib, I propose setting nested files and directories to the following default:

Answered by Jacksonkr

Post is based on https://stackoverflow.com/questions/31472755/sudo-npm-command-not-found