363

I'm trying to upgrade to the latest version of node. I'm following the instructions at http://davidwalsh.name/upgrade-nodejs

But when I do:

sudo npm install -g n

I get the error:

sudo: npm: command not found

npm works without sudo. When I do:

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 tried the solution at https://stackoverflow.com/a/5062718/1246159

But I'm still getting the same error. I also looked at the /etc/sudoers file and the relevant line is:

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

This looks fine to me. How can I possibly get NPM working with sudo command?

John Smith
  • 7,243
  • 6
  • 49
  • 61
Mark
  • 4,428
  • 14
  • 60
  • 116

30 Answers30

327

I had to do

sudo apt-get install npm

that worked for me.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Velocibadgery
  • 3,670
  • 2
  • 12
  • 17
  • 15
    It's hard to understand what you want to express. – tiny sunlight Jan 01 '16 at 18:29
  • 6
    This is out of date. What worked for me was these commands I found in here: https://github.com/nodesource/distributions#debinstall # Using Ubuntu curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_5.x | bash - apt-get install -y nodejs – Hamed Mar 27 '16 at 09:13
  • according to the https://docs.npmjs.com/getting-started/installing-node website this is still the supported way of installing/updating npm. If npm didnt get installed via nodejs which it didnt for some reason on my system. this is an accepted way of installing npm seperately. It is still compatible with the current version of nodejs. However if you are worried about the package being out of date you can download whatever version you wish with this https://registry.npmjs.org/npm/-/npm-{VERSION}.tgz link – Velocibadgery Apr 22 '16 at 01:56
  • 6
    @tinysunlight Im not sure what is unclear. The op says the npm command is missing. This command installs the npm command. – Velocibadgery Apr 22 '16 at 01:59
  • 46
    This is not the correct answer to the question because it is obvious from the op that `npm` is already installed but not reachable when invoked through sudo. With this solution you reinstall npm which is already existing. This is fighting symptoms rather than finding the cause. I understand that this might correct broken things for some people, but it is misleading, standing like this. – Dominic Feb 08 '17 at 15:43
  • For me the solution was this post: [On EC2: sudo node command not found, but node without sudo is ok](http://stackoverflow.com/questions/4976658/on-ec2-sudo-node-command-not-found-but-node-without-sudo-is-ok) – Dominic Feb 08 '17 at 15:54
  • This worked for me on 1.10.2018. I had Node 8.9.4 installed and working but I was getting the same error with NPM as the OP. Installed NPM again per this answer and the problem was solved. – Matt West Jan 10 '18 at 18:44
  • 9
    When I installed node using NVM on Ubuntu 18.10, sudo doesn't recognize npm as a command. `sudo apt-get install npm` worked for me. – AlienKevin May 28 '19 at 17:39
  • This is the only athorized version of installation. If it does not work it means npm ins not a part of distribution. You shlud consider carefully if You want use such software –  Nov 12 '20 at 16:20
175

The npm file should be in /usr/local/bin/npm. If it's not there, install node.js again with the package on their website. This worked in my case.

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
inga
  • 3,154
  • 1
  • 25
  • 29
  • thx. If you install node in other data drives, where os is not installed, you can not find this path. – Hardik Darji Feb 09 '18 at 07:32
  • 1
    in the case of mac os with homebrew.. two steps to do is to (1) which node (2) which npm which should be both in /usr/local/bin (where homebrew installs/symlinks node+npm)... to resolve.. try (1) brew doctor (2) brew reinstall node if its still giving issues.. check your path (echo $PATH). you must have multiple bin locations that point to various node installations..if that is the case... cleanup (as in make it neater) your ~/.bashrc / ~/.bash_profile, so /usr/local/bin is there loaded without another one pointing to a random node installation. – mirageglobe Mar 10 '19 at 18:15
  • 9
    This is a partial solution. The issue occurs when you manually install node. You must softlink each installed binary into /usr/bin or /usr/local/bin. Setting the path in root's bashrc will not work as it is not read during sudo therefore this is the only way it seems unless you install package. – shrimpwagon May 22 '19 at 14:02
  • 3
    as @shrimpwagon said, the bottom of manual instructions tell you to link "sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/node /usr/bin/node sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npm /usr/bin/npm sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npx /usr/bin/npx" – James Nelson Mar 11 '20 at 06:56
  • 2
    Why shoudl I use THEIR website? –  Nov 12 '20 at 16:17
  • getting it from the main website and add it to the PATH was the easiest way to handle this, the npm version from apt is an old one (you can add a point before the node folder to hide it in the file explorer) – younes zeboudj Mar 03 '23 at 15:58
137

For MAC users, the follow steps worked for me.

If you get Error for Brew, Here's the command you need to type first in the terminal:

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

Then run following commands:
$ brew update
$ brew uninstall node
$ brew install node
$ brew postinstall 
Hima
  • 1,249
  • 1
  • 14
  • 18
Gao
  • 1,527
  • 1
  • 9
  • 3
  • 11
    The `brew uninstall node` did not work for me, but I carried on with the `brew install node` and `brew postinstall` and it fixed the problem. Thanks – Trevor Aug 10 '17 at 12:40
  • 6
    What does `brew postinstall` do exactly? – Oliver D Feb 05 '20 at 14:16
  • 10
    in Homebrew version 2.2.14 `brew postinstall` is redundant – Arslan Khan Apr 30 '20 at 20:20
  • In my case, `brew install node` was unable to overwrite the symlink of an older version. I had to run `brew link --overwrite node` which fixed the problem. – not2savvy Feb 14 '22 at 14:54
91

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

  • sudo ln -s /usr/local/bin/node /usr/bin/node
  • sudo ln -s /usr/local/lib/node /usr/lib/node
  • sudo ln -s /usr/local/bin/npm /usr/bin/npm
  • sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
Vasanth Umapathy
  • 1,571
  • 14
  • 7
  • 3
    Worked for me on CentOS 7 – hirikarate Dec 30 '16 at 11:39
  • 7
    Worked for me Ubuntu 16.04.1 LTS. I use NVM so the first path was to where NVM installed node and npm (the path includes a version `%HOME%/.nvm/versions/node/v7.4.0/bin/npm`). Unfortunate that I will have to reference this answer each time I update using NVM due to how the version in the path will change – kas Jan 17 '17 at 13:52
  • 1
    This solved it for me as well! Originally posted on Stack Overflow: [On EC2: sudo node command not found, but node without sudo is ok](http://stackoverflow.com/questions/4976658/on-ec2-sudo-node-command-not-found-but-node-without-sudo-is-ok) – Dominic Feb 08 '17 at 15:59
  • 5
    @kas `sudo ln -s $(whereis node) /usr/bin/node` And so on. in your bash profile. – Aravind Voggu Nov 28 '17 at 08:24
  • Worked for me in AWS ec2 instance. – Chirag Purohit Aug 27 '18 at 08:08
  • I preferred this way instead of installing node again (I'm using binaries) – Pratik Charwad Jan 05 '19 at 15:31
  • I installed node using nvm and these two following commands worked for me. sudo ln -s ~/.nvm/versions/node/v10.15.3/bin/node /usr/bin/node && sudo ln -s ~/.nvm/versions/node/v10.15.3/bin/npm /usr/bin/npm – Semicolon Apr 07 '19 at 12:09
  • Copy and paste these commands won't fix it for some users like I who did not put nodejs in `/usr/local/bin/node` but rather you should point out out that we should link node or npm binaries found inside `bin` folder of nodejs folder(which can be somewhere like in `/usr/local/node-v14.17.0-linux-x64` or anywhere you extracted your nodejs folder). In my case bin folder containing `node` and `npm` binaries is found in `/usr/local/node-v14.17.0-linux-x64` . – Dilunga the Great May 12 '21 at 12:52
  • [This answer](https://stackoverflow.com/a/73308302/6243352) seems to be an improved version of this. – ggorlen Jun 01 '23 at 23:08
34

** EDIT **

WARNING! - Doing a chmod 777 is a fairly radical solution. Try these first, one at a time, and stop when one works:

  • $ sudo chmod -R 777 /usr/local/lib/node_modules/npm
  • $ sudo chmod -R 777 /usr/local/lib/node_modules
  • $ sudo chmod g+w /usr/local/lib
  • $ sudo chmod g+rwx /usr/local/lib

$ brew postinstall node is the only install part where I would get a problem

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 viola, npm is now linked

$ npm -v
3.10.10

Extra

If you used -R 777 on lib my recommendation would be to set nested files and directories to a default setting:

  • $ find /usr/local/lib -type f -print -exec chmod 644 {} \;
  • $ find /usr/local/lib -type d -print -exec chmod 755 {} \;
  • $ chmod /usr/local/lib 755
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
23

I had to do the following:

  1. brew update
  2. brew uninstall node
  3. Visit https://nodejs.org/en/ download the file
  4. Install the downloaded file
wpp
  • 7,093
  • 4
  • 33
  • 65
maudulus
  • 10,627
  • 10
  • 78
  • 117
14

If you installed node/npm with nvm, the nvm environment configuration file has to be run before you can use either package.

This is generally found in ~/.nvm/nvm.sh.

To run this automatically, include:

source ~/.nvm/nvm.sh

in the .bash_profile file for your user

If you then want to use sudo with that user, make sure to include the -i parameter to make sudo set the user environment. eg

sudo -iu jenkins npm install grunt-cli
Garreth McDaid
  • 2,427
  • 22
  • 34
13

You can make symbolic link & its works.

  1. find path of current npm

which npm

  1. make symbolic link by following command

sudo ln -s **which/npm** /usr/local/bin/npm

  1. Test and verify.

sudo npm -v

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39
11

In order to install npm packages globally with sudo permission, /usr/bin/npm should be available. If npm exists on some other directory, create a soft link like:

sudo ln -s /usr/local/bin/npm /usr/bin/npm

It works on Fedora 25, node8.0.0 and npm5.0.0

Prakash Ramasamy
  • 385
  • 1
  • 7
  • 15
11

On macOS, this worked for me:

brew reinstall node
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
  • 2
    Sorry just noticed the question is tagged with `unix`, but it might still be helpful since it's the top Google search result for "macos npm command not found" – friederbluemle Nov 02 '19 at 01:33
8

For CentOS users, this works for me:

sudo yum install npm
frankchen0130
  • 559
  • 6
  • 7
  • 1
    the problem with this approach is that in Fedora for example, it is very outdated in dnf / yum repos – ACV Feb 28 '16 at 18:37
  • 3
    On CentOS 7 you need to enable EPEL repo first: `yum install epel-release; yum install npm` – hudolejev May 24 '17 at 13:00
7

For debian after installing node enter

curl -k -O -L https://npmjs.org/install.sh    
ln -s /usr/bin/nodejs /usr/bin/node  
sh install.sh
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Shaharyar Zafar
  • 104
  • 1
  • 8
6

You can resolve this by creating symbolic links for npm and node which is quick and simple:

sudo ln -s $(which npm) /usr/local/bin/npm
sudo ln -s $(which node) /usr/local/bin/node

I'm using which to get the location and then passing that in as a variable to the command e.g. $(which npm).

I'm creating a symbolic link for node as well because npm depends on node.

Then you can just:

sudo npm -v

No need to reinstall node or go wild with chmod on whole directories.

Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
5

simply reinstall .

On RHEL, CentOS and Fedora

sudo yum remove nodejs npm
sudo dnf remove nodejs npm   [On Fedora 22+ versions]

then

yum -y install nodejs npm
dnf -y install nodejs npm   [On Fedora 22+ versions]

easy!.. both node and npm works like a charm now!

Brian Sanchez
  • 832
  • 1
  • 13
  • 11
2

I resolved this problem by

apt-get install npm2deb
  • 3
    what is this answer? npm2deb is a 3rd party – Beau Bouchard Oct 04 '17 at 15:56
  • 1
    it's not entirely "trusted" as in 3rd party can be more likely compromised than from npm directly from the source. i don't know anything about npm2deb but think about if you wanted to use it at your company, they'd probably be strict about auditing it – Kyle Calica-St Oct 10 '17 at 03:37
2

My solution is:

sudo -E env "PATH=$PATH" n stable

Works fine for me.

Found it here: https://stackoverflow.com/a/29400598/861615

This happens because you have change default global packages directory

xargr
  • 2,788
  • 1
  • 19
  • 28
  • 1
    This is the only answer that doesn't introduce tons of other problems (like having two installations, messing up permissions, etc etc.) – derpedy-doo Aug 20 '23 at 14:48
1

If you have downloaded node package and extracted somewhere like /opt you can simply create symbolic link inside /usr/local/bin.

/usr/local/bin/npm -> /opt/node-v4.6.0-linux-x64/bin/npm
/usr/local/bin/node -> /opt/node-v4.6.0-linux-x64/bin/node
mipasov
  • 293
  • 2
  • 8
1

Work for me Resolving EACCES permissions errors when installing packages globally

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use hidden directory in your home directory.

Back up your computer. On the command line, in your home directory, create a directory for global installations:

 mkdir ~/.npm-global

Configure npm to use the new directory path:

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

In your preferred text editor, open or create a ~/.profile file and add this line:

 export PATH=~/.npm-global/bin:$PATH

On the command line, update your system variables:

 source ~/.profile

To test your new configuration, install a package globally without using sudo:

 npm install -g jshint
eustatos
  • 686
  • 1
  • 10
  • 21
1

So, for those using:

NVM and homebrew

make sure to set node version. For me, my node version was no longer set. So, I checked what versions I had using

nvm ls

this listed v16.13.1, so I set it to use this

nvm use 16

once my node version was set, npm commands worked again

Daltron
  • 1,729
  • 3
  • 20
  • 37
0

I had the same issue,the reason for it was npm package manager was not installed while installing node. This was caused because of the following mistake: In the installation process there is a step called "Custom Setup", here you have the option to choose one of the following: 1) Node.js runtime (This is selected by default). 2) npm package manager 3) Online documentation shortcuts. 4) Add to Path. If you proceed as it is npm package manager will not be installed and hence you will get the error.

Solution: Select npm package manager when you get these options. This worked for me.

Saurabh Shetty
  • 395
  • 1
  • 5
  • 14
0

Appended npm binary path to sudo path using visudo and editing "secure_path"

Now "sudo npm" works

mauricio777
  • 1,296
  • 1
  • 15
  • 15
0

I also had the same issue in Homestead and tried many ways. I tried with

sudo apt-get install nodejs

I get the following error:

The following packages have unmet dependencies:
 npm : Depends: nodejs but it is not going to be installed
       Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Finally I tried with

sudo apt-get dist-upgrade

It worked fine.

root@homestead:/usr/local/bin# npm -v
3.10.10

root@homestead:/usr/local/bin# node -v
v6.13.0
Striped
  • 2,544
  • 3
  • 25
  • 31
Thowfeek
  • 137
  • 1
  • 5
0

Since I have installed node.js using .tar file available on node.js, I had to put the location of the node directory on:

~/.bashrc

of root by changing from normal user to root using command:

sudo -i

then I had to add the path to node where I extracted it into .bashrc file as below: enter image description here

then refereshed .bashrc using

. ~/.bashrc

there after

npm: command not found

went away

MOHAMMAD WASEEM
  • 185
  • 1
  • 6
0

Instead of Installing node again which seems like the accepted solution, The Problem is there are no permissions to the nodejs folder/usr/local.
Enter the following command sudo chmod -R 777 /usr/local/nodejs/

AEM
  • 1,354
  • 8
  • 20
  • 30
tahervali
  • 29
  • 4
0

In my case, for some reason after installing some python modules, I suddenly start getting messages saying node is not installed or is not recognized as a command. Upon checking, my path is registered in .bashrc. So, I sourced that file, and everything started working again.

source .bashrc

Mithun Adhikari
  • 521
  • 6
  • 13
-1

Remove Node completely:

  brew uninstall --force node

Install again:

brew install node;
which node # => /usr/local/bin/node
export NODE_PATH='/usr/local/lib/node_modules'
ASHISH R
  • 4,043
  • 1
  • 20
  • 16
-1

In case could be useful for anyone that uses rh-* packages this worked for me:

sudo ln -s /opt/rh/rh-nodejs8/root/usr/bin/npm /usr/local/bin/npm
Lionel T
  • 1,559
  • 1
  • 13
  • 30
-2

If you are using linux delete node_modules folder that exists in /usr/lib

sudo rm -rf node-modules

then reinstall node using curl:

sudo apt-get update
sudo apt install curl build-essential
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
  • 6
    It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Mar 11 '22 at 05:43
-3

My workaround was to login as root and now I don't have to use sudo again

su root
Shalabyer
  • 555
  • 7
  • 23
-15

Installl node.js & simply run

npm install -g bower 

from your project dir