29

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

8 Answers 8

42

I have found solution which worked for me:

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

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

3
  • 1
    env: n: No such file or directory Jul 15, 2016 at 19:10
  • 2
    @user3311522 did you use: sudo npm install n -g before sudo -E env "PATH=$PATH" n stable ??
    – Luckylooke
    Jul 17, 2016 at 19:13
  • life saver! Thanks!!
    – n13
    Mar 14, 2022 at 12:03
8

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
4
  • 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, 2015 at 19:19
  • Didnt help, still same isssue :/
    – Luckylooke
    Jul 6, 2015 at 14:53
  • This helped me! I didn't have ~/.npmrc file, so I added it and now it works fine Sep 6, 2016 at 12:10
  • 3
    Beware: this will undo Option 2 of fixing NPM permissions Jan 10, 2017 at 21:55
5

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
5

This mean you don't have N installed in you system, N is an npm-based node version manager. You can install it by installing first some version of node and then running

sudo npm install -g n

now you can install the version on node using n command

sudo n stable

The above will install the latest stable verion

sudo n lates

this will install the lates version

you can also install the specific version by specifying the version as below

sudo n  10.16.0
3

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

0

None of the answers above worked for me. This however this did:

sudo /usr/local/bin/bin/n stable
-1

I encountered the same problem, this is how you can solve it:

  1. Get npm config prefix
    npm config get prefix 
    // get -> ${myhome}/.npm-global
    
  2. Add the following lines in .zshrc

    # Specify `n` to put the version of `node` in the `.n` directory.
    export N_PREFIX=${myhome}/.npm-global/.n
    
    # Make `nman` command work
    export PATH=${myhome}/.npm-global/bin:$PATH
    
    # Add this line to use `node -v` command
    export PATH=${myhome}/.npm-global/.n/bin:$PATH
    

Reference: GitHub - tj/n: Node version management

Editor's note: This answer was originally written in Chinese and has been translated and edited using online tools. To see the original answer, refer Revision #1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .