Upgrade Node.js via NPM

By  on  

This method of upgrading node is now unstable and should not be used. The best way to manage Node.js versions is to use NVM: Node Version Management.!

I was recently installing a utility via NPM when I learned that my version of Node.js itself was out of date.  No worries -- simply upgrade my Node.js install and move forward.  Of course I could just hit nodejs.org and get the new image, but figured there had to be an easier way.  It turns out there is -- you can upgrade your local Node.js with NPM:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

The n package represents a Node helper, and running the last command upgrades node to the latest stable version.  Instead of using "stable", you could specify a desired version:

sudo n 0.8.21

Once your install is complete, you can confirm you version with another command:

node -v

It's quite nice that you can upgrade Node.js right from npm;  it's like Inception...or something.

Recent Features

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Create Twitter-Style Dropdowns Using jQuery

    Twitter does some great stuff with JavaScript. What I really appreciate about what they do is that there aren't any epic JS functionalities -- they're all simple touches. One of those simple touches is the "Login" dropdown on their homepage. I've taken...

  • By
    prefers-color-scheme: CSS Media Query

    One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.

Discussion

  1. Chris Hamant

    Just to be pedantic – ‘n’ isn’t something that the guys behind npm and nodejs core team created nor necessarily support.

    ‘n’ the package ‘https://github.com/visionmedia/n’ is created and maintained by the prolific TJ Holowaychuk. It’s one of many competing version managers for nodejs – https://github.com/creationix/nvm being another.

    While I do sometimes need to manage different versions of node (those tools are convenient for sure) I personally rely upon a decent package manager on my OS. ‘Homebrew’ (or MacPorts/Fink if a masochist) on OSX. ‘Chocolatey’ on Windows. (Linux users probably have it in their distro’s package manger)

    Installing via the provided packages will ensure that npm itself is up to date too (which not all other solutions do).

    • alexserver

      alright, so you recommend to brew update node ?
      nice to hear.

    • Jim

      The versions on the package managers are often woefully out-of-date. Tools like n and nvm allow for the latest, either via source (nvm) or binaries (n).

  2. Neat, but I prefer to build from source and install to ~/.local so I don’t have to sudo.

  3. Mehdi

    emmmm… nvm?

  4. Santi

    Anyway of doing this on windows?

    • @Santi have you found the way to update node on windows ?

    • priya

      I found it.

      npm install npm@latest -g
      
  5. John

    Didn’t work for me.. :(

  6. Paul

    Thanks, this worked great for me.

    Regardless of whether ‘n’ is developed by the npm guys, I haven’t done anything special and I found it was already installed on my centos 6 system (which was set up from scratch and only has node and npm installed)

  7. Rob

    Thanks David, you are a wealth of information!

  8. MattDMo

    It’s nice to know about n, but one of its major failings (at least for me, on Linux, without ‘sudo’ privileges) is that there’s no way to define a local install path, or even one that’s not /usr/local. Back to building the source for me…

    • Laubstein

      Try that:

      export N_PREFIX=; n stable
      
    • Laubstein

      Ops…

      export N_PREFIX=/destination/dir; n stable
      
  9. David Beckwith

    It worked nicely for me. Thank you!

  10. Thanks worked for me.

  11. Works fine thx!

  12. ilker

    Did anybody get this working on Windows 7 ?
    When trying to run n (anything with n , like “n -h” or “n stable” fails saying
    ‘sh’ is not recognized as an internal or external command.

    Because in AppData\Roaming\nmp\n.cmd file there is a line trying to use sh as
    sh “%dp0\node_modules\n\bin\n” %*

    and since it does not know what sh is it does NOT work on windows 7.
    Looked at the package.json file of n , did not see any dependencies.

    What is missing ? How to get the n to work on Windows 7 ?

    • Hey !
      Did you find a workaround ?
      Cheers :)

    • you gotta download CMDer and Cygwin
      CMDer is a unix style command line. It lets you use stuff like “sh”, “ls”, stuff Linux can use.
      Cygwin lets you run bash stuff, like ‘bash init.sh’.

  13. worked for me.. cheers

  14. Tony

    I’ve been using n for sometime now and like it, seems like everyone uses nvm but I like the stuff TJ makes, he’s brilliant

  15. Borislav

    It worked like a charm! Thank you!

  16. Worked like a charm – cheers mate

  17. Vamshi

    this worked cool.. thanks

  18. Yeah, trying to figure out on windows 7 too, if anybody found a way :)
    Same behavior as iliker.

  19. It worked for me… but it killed npm. That is to say, node was update but npm command stopped working. I had to reinstall node the old way to get npm working again.

  20. Worked for me, thanks Mr Walsh!

  21. Muchas gracias! funciona perfecto!

  22. Daniel Mejia

    Gracias david. I found out about cache clean.

  23. Very useful and fast. Many thanks!

  24. mac

    I’m new in node development and just starting to learn from it.. I had an issue with npm add user and I decided to update node using the following command but after updating.. whenever I run node or npm I always got “Floating point exception” on my terminal. Please help.

    I’m using SUSE Linux sp1.

  25. Ian

    Thanks for the info. On Windows 7 installing n worked for me but then it tells me the version it tries to install is a invalid version. Since it is the very version I get from nodejs as a installer, I’m going to just say this does not work on Windows but it’s no big deal since it’s so easy to download the installer from the website.

    Will probably use this on my Linux machines if I remember so thanks :-)

  26. Carl-Erik

    Does not work on Windows 7.

    $ n stable

    install : v0.10.26

    Error: invalid version 0.10.26

    • At least it’s a version issue and not necessarily a node/n issue.

  27. Lucidz

    No version works with n in windows. They all give invalid version.

    • Luke

      Could it be n.exe on Windows perhaps?

  28. marcelklehr

    Hey guys.

    Just to clear things up: N doesn’t support windows. It works Unixy operating systems, but not on windows.

    If you need a node version manager for windows, try https://github.com/marcelklehr/nodist

    cheers!

  29. Carlos Vazquez

    Thanks for the instructions, it was the only way I could have possibly pulled this out (I’ve read way too many articles)

  30. Loren Amelang

    WARNING!
    DON’T DO THIS if you are not on an x86 machine!

    I’m on ARM (BBB) and this procedure stupidly installed the x86 binary over my existing ARM version. I’m now three hours into repairing the damage.

    I knew there was trouble when I saw the response to the first command:
    ubuntu@ubuntu-armhf:~/Ldrv$ sudo npm cache clean -f
    npm WARN using –force I sure hope you know what you are doing.

    But there was no opportunity to back out by the time the message was up. Maybe if I’d stopped right there I wouldn’t be re-installing from scratch now?

    (Yes, I could apt-get a version quickly, but the repo version is too old to do https!)

    Please, David, put a big “Warning – x86 ONLY” message at the top of your page!

    Loren

    • Yasin Alpay

      I don’t know what I am doing! I trusted this post helps, and helped :)

  31. Absolux

    hi, it works fine for me. thank you.

    i has a troubleshotting with the npm. it doesnt recognize the registry http://registry.npmjs.org/. so before typing
    ! sudo npm install -g n
    you should type :
    $ npm config set registry http://registry.npmjs.org/

    PS: Curl must be installed also.

    thank you again.

  32. Connor

    After following this tutorial, my node installation got totally fucked. Required core node mudules such as ‘fs’ causes errors. Don’t use this method.

    • God Shamgod

      As did mine. Too bad your comment was at the end of the list.

  33. Avindra Goolcharan

    Heads up, this does not work for io.js. If you try this with io.js, your binaries will be replaced with the latest node.js bins.

  34. God Shamgod

    Do not use this to update to 0.12.0 !

    node-gyp will refuse to work.

  35. I use AWS cloud service and everytime I use the clean cache command it messes with the server and I have to restore from a back up.
    do I have to clean the cache to be able to update my node version?
    how would i go about doing this without using
    sudo npm cache clean -f

    because everytime i use it i get this error:
    kernel:BUG: soft lockup – CPU#1 stuck for 22s! [npm:1206]

  36. nodeUser

    This will also fuck up the permissions in your .npm directory, preventing you from installing any other packages.

  37. Narendra

    Very useful. It’s working fine for me. Many thanks!

  38. nodeguy

    DO NOT DO THIS!!! This is a great way to fubar your install and require hours of cleanup. Thanks a lot man!

  39. none

    what about windows?

  40. Al

    Thanks mate, it worked

  41. Girmay

    This will fuck up your setup! Now nodejs can’t find my modules even if my module poked it in the eye.

  42. WARNING: THIS FUCKS UP YOUR NPM SETUP !!! DO NOT DO THIS !!! (OS X)

    What I believe it does is, if not anything else, it changes the npm path to /Users//npm which should be /usr/local/bin by default.

    The link below helped me fix it.
    http://stackoverflow.com/questions/14803978/npm-global-path-prefix

  43. george

    Hi Dave, please pardon me for asking but I am learning coding and came to notice this on my Ubuntu system and needed a good explanation for it. I do [node –version] and get v5.0.0, then do this [nodejs –version] and get v0.10.25. Question: is node NOT the same with nodejs.
    Please clear this for me

  44. What worked for me on Ubuntu is the following:

    curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    Upgraded from v0.10.25 to v4.2.2

  45. Alistair

    You might want to avoid going this with sudo. If you can’t do it without sudo, there might already be something amiss with your node setup.

  46. Geopelia

    Nice trick, now i dont need to have reinstall anything or have 2 different versions of node

  47. For windows, just download the installer from nodejs.org .-.

  48. Work’s like a charm tested on 12/3/16

  49. Christian Haller

    Works on my Mac too (El Capitan 10.11.3)

    ➜  ~ npm install -g n
    ➜  ~ n stable
    
         install : node-v5.8.0
           mkdir : /usr/local/n/versions/node/5.8.0
           fetch : https://nodejs.org/dist/v5.8.0/node-v5.8.0-darwin-x64.tar.gz
    ######################################################################## 100,0%
       installed : v5.8.0
    
    ➜  ~ node -v
    v5.8.0
    
    
  50. Jeremy Martin

    This tutorial is out of date. I recommend using nvm (node version manager) by Tim Caswell – a NodeJS community leader. He and and 30 other contributors created a nodejs version manager which is well documented an current.

    https://github.com/creationix/nvm

  51. Thanks for the correct and concise post, it got me updated in a jiffy!

  52. Aravind

    Awesome!! It worked for me. Only suggestion is, if you are running commands a root user, just don’t use the sudo keyword.

  53. AWESOME, thanks a lot.

    I was using v0.10.4.

  54. Mary

    I have problem with upgrade node.js via npm I have seen Error Invalid version 7.7.3. Thank you for your help.

  55. This worked for us. Thanks!!!

  56. Neeraj

    You made my day :)

  57. Lars

    Ah this is the way to do it nowadays. https://stackoverflow.com/a/33010229/2768271

  58. The n package does not work on windows10 64bit

  59. joeri

    Yes and then open a new shell.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!