12

I'm trying to update my version of node to the latest stable.

Using this resource I was able to:

sudo npm install n -g

But when I try

sudo npm n stable

I get:

sudo: n: command not found

If I run n stable, the command is present:

n stable
mkdir: cannot create directory ‘/usr/local/n’: Permission denied
mkdir: cannot create directory ‘/usr/local/n’: Permission denied
mkdir: cannot create directory ‘/usr/local/n’: Permission denied
mkdir: cannot create directory ‘/usr/local/n’: Permission denied

     install : node-v0.12.2
       mkdir : /usr/local/n/versions/node/0.12.2
mkdir: cannot create directory ‘/usr/local/n’: Permission denied

  Error: sudo required
  • Nice idea, but when I do that it returns me 'n: command not found', even though I've dropped the 'sudo' – JonRed Apr 13 '15 at 8:15
  • sudo sh -c "PATH=$PATH; n stable" <new line> sh: 1: n: not found – JonRed Apr 13 '15 at 8:19
  • No output at all. That's depresssing :-( – JonRed Apr 13 '15 at 8:20
  • Let us continue this discussion in chat. – JonRed Apr 13 '15 at 8:21
  • Please post the output of ls /usr/local/bin/n – Helio Apr 13 '15 at 8:41
5

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 think this nailed it! It also helped me just now when I updated my npm version, but wouldn' t recognise it. So, accepted, and thank you! – JonRed Apr 19 '15 at 19:19
  • Didnt help, still same isssue :/ – Luckylooke Jul 6 '15 at 14:53
  • This helped me! I didn't have ~/.npmrc file, so I added it and now it works fine – Kirill Gusyatin Sep 6 '16 at 12:10
  • 2
    Beware: this will undo Option 2 of fixing NPM permissions – Edson Horacio Junior Jan 10 '17 at 21:55
22

I have found solution which worked for me:

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

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

  • env: n: No such file or directory – user3311522 Jul 15 '16 at 19:10
  • @user3311522 did you use: sudo npm install n -g before sudo -E env "PATH=$PATH" n stable ?? – Luckylooke Jul 17 '16 at 19:13
2

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
0

To avoid messing up with the .npm-global folder as noted by @Edson Horacio Junior, and based on @pohest's answer, here is how I fixed it:

sudo ln -s /home/<username>/.npm-global/bin/n /usr/local/bin/n
sudo n --version # test it

As a note, this probably will not work if you haven't followed npm's steps for changing the default global directory.

0

I found a solution that worked for me. Use nvm package to download latest node.

  1. Install nvm using below command

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

  1. Create an environment variable to use nvm
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  1. Try nvm -v if it cannot see nvm installed, run the script below

command -v nvm

  1. To install node run command below

nvm install node

  1. After restarting the terminal, try to check the version of node, it should be updated

node -v

More info here: https://github.com/nvm-sh/nvm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.