Command not found when executing node.js n package on sudo

I have found solution which worked for me:

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

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


Surprisingly, your npm installation has the global prefix in a folder called npm on your home directory, this means that any package installed with the -g flag will install on this folder.

You can change this folder to any folder that is on the sudo safe path following these steps:


Graphical way:

  1. Open a File Manager (a.k.a Nautilus).
  2. Navigate to your home folder.
  3. Press Ctrl+H to show hidden files.
  4. Open a file called .npmrc with your favorite text editor.
  5. Find a line on that file with this content:

    prefix=/home/<your_username>/npm
    
  6. Replace /home/<your_username>/npm by a safe path (such as /usr/local/bin).
  7. Once replaced it will look like this:

    prefix=/usr/local/bin
    
  8. Save the file.
  9. Run again sudo npm install n -g

Terminal way:

Run this command:

sed -i.bak "s%^prefix=.*$%prefix=/usr/local/bin%" ~/.npmrc

I know this is an Ubuntu forum, but I'm sure this will help someone with the same problem on the RHEL flavours who Googled to here like I did. Perhaps it also works in Ubuntu.

This is the approach:

ln -s /usr/local/bin/n /usr/bin/n

Tags:

Nodejs

14.04