Run Node.js and NPM with Sudo (Fix "Command not Found" with NVM)

mail
Cyrus Kao
Last modified

While using NVM on Linux, it loads Node.js and NPM from .bashrc in the home directory of your user. However, if you execute the command with sudo like sudo npm start it will result in sudo: npm: command not found since .bashrc of superuser is not managed by NVM.

.bashrc
.bashrc that loads NVM

Solution

To resolve this problem, we'll find out the real location of Node.js and NPM so no .bashrc is needed:

which node
which npm
Bash
/home/arch/.nvm/versions/node/v17.0.1/bin/node
Output
/home/arch/.nvm/versions/node/v17.0.1/bin/npm
Output

Then execute the binaries with sudo:

sudo /home/arch/.nvm/versions/node/v17.0.1/bin/node index
sudo /home/arch/.nvm/versions/node/v17.0.1/bin/npm start
Bash

See Also

Comments

0500