[node.js] sudo: npm: command not found

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?

This question is related to node.js linux unix npm

The answer is


WARNING (edit)

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

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.


Installl node.js & simply run

npm install -g bower 

from your project dir


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

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

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

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 

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


For CentOS users, this works for me:

sudo yum install npm

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

You can make symbolic link & its works for me.

  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


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

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

For me, any of the methods mentioned above using Homebrew did not work on macOS. So, I uninstalled node using Homebrew and downloaded the node package from https://nodejs.org/en/download/ and installed it. It worked like a charm.


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!


On macOS, this worked for me:

brew reinstall node

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


I had to do

sudo apt-get install npm

that worked for me.


I resolved this problem by

apt-get install npm2deb

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


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'

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

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

Now "sudo npm" works


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

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?