/ Ubuntu

Simple script to upgrade Node.js with nvm [update]

I have updated the script, if we use only 1 argument, it will auto detect current Node version and upgrade from that. So, make sure your argument is the latest, or at least newer than your current version.

NVM is a great tool to control Node.js, but I keep forgetting its command, so I created this simple bash function to update.

The script will do 3 jobs in order:

  1. Install new Node version the reinstall all packages from old Node version.
  2. Change current Node to new version and uninstall the old.
  3. Update npm to latest version.

Open your .bash_profile then add:

do_nvm() {
    nvm install $2 --reinstall-packages-from=$1;
    nvm use $2;
    nvm uninstall $1;
    npm i -g npm;

upnode() {
    if [[ "$#" -eq 1 ]]; then
        do_nvm $(node -v) $1;
    if [[ "$#" -eq 2 ]]; then
        do_nvm $1 $2;
    if [[ "$#" -gt 2 ]]; then
        echo "Can not use more than 2 arguments."

Source your file, (or restart terminal) next time use it like this:

upnode old_version new_version


upnode new_version

Remember: use exactly version in vX.X.X, e.g.:

upnode v8.8.0


[x] Accept 01 argument and upgrade from current version.
[ ] Detect both vX.X.X and X.X version only.
[ ] Compare verions from input.
[ ] Have options: keep old versions; auto swap version numbers if user input wrong position (Use the compare.)