How Do You Use “sudo npm” or “sudo node” Commands?

Problem scenario
You run "sudo npm" or "sudo node" and you get "command not found" error messages.  What should you do?

Modify the /etc/sudoers file.  Find the stanza with secure_path.  Append ":/usr/local/bin" (with no quotes) to the line.

Here is an example of how the stanza should look (it is often on line 80 of /etc/sudoers):

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

Once the /etc/sudoers file has been saved, the effects should be seen immediately.  There is no need to log out and log back in.

If you are very conservative with security, verify you are using sudo 1.7 or higher.  Run this command to determine your sudo command version:

man sudo | grep Sudo | awk '{print $2}'

sudo -V
 # This command will also work

It is extremely unlikely, but if you have a very old Linux server or some strange configuration, your sudo could be too old for this solution.  To learn why you should be using sudo 1.7 or higher, you can research these vulnerabilities:  CVE-2005-2959, CVE-2005-4158, and CVE-2006-0151.

Leave a comment

Your email address will not be published. Required fields are marked *