In the dynamic landscape of web development, staying current with the latest versions of Node.js is crucial for performance, security, and compatibility. Node Version Manager (NVM) is an essential tool for simplifying this process. This article provides a comprehensive guide on using NVM to upgrade Node.js, ensuring a smooth and efficient workflow for developers.

Advertisement

Understanding NVM

Node Version Manager (NVM) is a command-line utility that enables you to manage multiple Node.js versions. It’s especially useful in environments where different projects require different Node.js versions, allowing you to switch between them effortlessly.

Why Use NVM for Upgrading Node.js?

  • Versatility: Manage multiple Node.js versions on a single machine.
  • Convenience: Easily switch between Node.js versions as per project requirements.
  • Safety: Test new versions without affecting existing setups.
  • Simplicity: Streamline the upgrade process with straightforward commands.

Installing NVM

Before upgrading Node.js, you need to install NVM. Follow these steps:

  1. Open your terminal.
  2. Run the installation script:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash 
    

    (Visit the NVM GitHub page for the latest version.)

  3. Restart your terminal or run source ~/.bashrc (or equivalent for your shell) to apply the changes.
    source ~/.bashrc 
    

Upgrading Node.js with NVM

With NVM installed, upgrading Node.js is straightforward.

  1. Check Available Versions:
    List all available Node.js versions with:
    nvm ls-remote 
    
  2. Install a New Version:
    Install the desired version (e.g., v20.0.0) using:
    nvm install 20.0.0 
    
  3. Switch to the New Version:
    Change to the newly installed version with:
    nvm use 20.0.0 
    
  4. Set Default Node Version:
    Set the default Node.js version for new terminals:
    nvm alias default 20.0.0 
    

Verifying the Upgrade

Confirm that the upgrade was successful by checking the Node version:

node -v 

Best Practices for Upgrading

  • Read Release Notes: Before upgrading, review the release notes for potential breaking changes.
  • Backup Projects: Always backup your projects before upgrading.
  • Test Thoroughly: After upgrading, test your applications to ensure compatibility.

Troubleshooting

  • Installation Issues: Ensure your shell is supported and the installation script ran without errors.
  • Version Not Found: Check for typos in the version number or update NVM.
  • Switching Versions: If nvm use doesn’t reflect changes, ensure your shell’s configuration file is correctly set up.

Conclusion

NVM is a powerful tool that simplifies the process of managing and upgrading Node.js versions. By following these steps, developers can maintain an efficient and flexible development environment. Remember to stay informed about the latest Node.js releases and upgrade regularly to leverage new features and improvements.

Further Resources:

Share.

45 Comments

  1. Hi, Having an issue here.
    sudo n lts
    appears to work correctly
    installing : node-v14.18.1 …
    It told me it changed location…
    old : /usr/bin/node
    new : /usr/local/bin/node
    But now I don’t have node.
    node -v
    bash: /usr/bin/node: No such file or directory

    Thank you for any advice

  2. Hi, thanks for this tutorial!

    I had to do : $ NODE_MIRROR=http://nodejs.org/dist/ n –lts instead of : $ sudo n stable.

    At the end, I did : $ sudo ln -sf /usr/local/n/versions/node/10.16.0/bin/node /usr/bin/node.

    When I check the version, I have this message : bash /usr/bin/node no such file or directory.

    Thanks for your answer.

  3. I updated to node 12.2.0, however when I run “node -v”, still shows the previous version. And the command “sudo ln -sf /usr/local/n/versions/node/12.2.0/bin/node /usr/bin/node” doesn’t help.

    • Jone, First check which node binary is default executed…

      $ which node
      

      Then you can symlink /usr/local/n/versions/node/12.2.0/bin/node to that binary file.

  4. After typed this command, when I typed “node -v” show error “no such file or directory”. Give me a help please!
    ln -sf /usr/local/n/versions/node/6.0.0/bin/node /usr/bin/node

  5. Thank you very much for this! I was trying to install LESS but was having a hard time. It turns out it’s just the version compatibility with my node. I finally get it to work.:D

  6. udayan debnath on

    Thanks man….. You are my hero …. There are many option to do that all over the net including nodejs.org. But i understand that once npm is installed as a package manager then it should be done by npm itself. I used fedora 24 with dnf to install the default package from the repo. Then upgrade npm and then node…. thanks again…. now i am learning npm cli command before anything.

  7. Is this powershell, command prompt…(windows,Mac,Linux) sudo suggest linux ..so probabbly mac lynux. node -v is the only thing that worked for me. Should i just download the node windows install and run tha

  8. Hi, I hope you can help me out. I tried to upgrade my Pi but I still have an older version of nodejs:

    pi@raspberrypi:~ $ node -v
    v5.6.0

    pi@raspberrypi:~ $ nodejs -v
    v0.10.29

  9. wtf this just broke the node command for me… when I punch in node -v or any node command now it gives me /usr/bin/node: No such file or directory. awesome.

  10. Note that the line:
    sudo ln -sf /usr/local/n/versions/node/5.0.0/bin/node /usr/bin/node
    will need the 5.0.0 version number updated to match the stable version that was installed.

  11. Doesn’t work on windows:

    npm ERR! notsup Unsupported
    npm ERR! notsup Not compatible with your operating system or architecture: n@2.1
    .0
    npm ERR! notsup Valid OS: !win32
    npm ERR! notsup Valid Arch: any
    npm ERR! notsup Actual OS: win32
    npm ERR! notsup Actual Arch: x64

Exit mobile version