Coder Perfect

npm: command not found in sudo


I’m attempting to upgrade node to the most recent version. I’m following’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


which npm



I attempted to solve the problem at

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"
$ 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

$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node

and there you have it, npm is now linked.

$ npm -v


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