DEV Community

Mumenya Nyamu
Mumenya Nyamu

Posted on

How To manage Different Versions of Node in Your system

Node Logo

Web developers need npm(node package manager) to manage resources, be it Angular, React, React-frameworks(Next.js and Remix), or any frontend resource.
npm ships with Node.js.

To get started you need to install Node from the official documentation. Download the version specific to your operating system.

To update node and npm to the latest versions, Dillion Megida has a great article at freecodecamp.

There is so much more to Node than just npm however for this article, I want to focus on the management of node or npm versions.

Some reasons you may want to have different node versions are:

  • Compatibility: Some applications or projects may require a specific version of Node.js to run properly. Having multiple versions of Node.js installed on your system allows you to switch between them as needed, depending on the specific requirements of the project you are working on. For example the Angular CLI only uses lts(long-term support) versions of Node, and you may want the latest version which has the latest packages.

  • Testing: If you are a developer, you may need to test your code on multiple versions of Node.js to ensure that it works correctly on different environments. Having multiple versions installed on your system allows you to test your code against different versions without having to set up separate virtual machines or other environments.

  • Dependency Management: If you are working on a project with multiple developers, each of them may have a different version of Node.js installed on their system. To ensure that everyone is working with the same version of Node.js, you can specify the required version in your project's package.json file. This way, when someone installs your project's dependencies, they will automatically get the required version of Node.js installed.

You can check your node version by running

node -v
Enter fullscreen mode Exit fullscreen mode


an alias for node --version

To manage Node I recommend using two resources. This will help you install as well as use the node version of your choice.

  1. You can use the node package n
  2. You can use nvm the node version manager.

1 - n is a tool that allows you to easily switch between different versions of Node.js. Follow the official guide in case this does not work for you due to an update or need a command not highlighted below.
Here's how you can use n to switch between Node.js versions:

  • Install n globally using npm:

    npm install -g n
    
  • List the available versions of Node.js using n:

    n ls
    


    This will display a list of all the Node.js versions that are currently installed on your system.

  • To switch to a specific version of Node.js, use the n command followed by the version number:

    n <version>

    For example, to switch to Node.js version 14.17.0, you can use the command:

n 14.17.0
Enter fullscreen mode Exit fullscreen mode


This will download and install the specified version of Node.js, and then switch to it. To verify that you're now using the correct version of Node.js, you can run the node -v command:

node -v
Enter fullscreen mode Exit fullscreen mode


This should display the version number of the Node.js version that you just switched to.

  • To switch back to the system default version of Node.js, you can use the n command without any argument:

      n
    

This will switch back to the system's default version of Node.js.

  • Other commands you might find useful:
    • To install the latest version for long-term support n lts
    • To install the latest version n latest
    • To remove some cached versions n rm 0.9.4 v0.10.0
    • To remove all cached versions except the installed version n prune
    • To see remote versions available for download:
      • n ls-remote lts for the latest lts remote version available
      • n ls-remote latest for the latest remote version available
      • n lsr <version num> to see all sub-versions produced for a particular version . can use n lsr 16 to see all sub-versions under version 16.
      • n --all lsr for all remote versions available

2 - Use nvm the node version manager.
This is very similar to n. Its installation however requires you to use the scripts or terminal. In macOS you can use homebrew to install it. For different OS however, its best to follow the official guide.
Upon installation to manage different node versions:
- nvm install <version> installs a specific version.
- nvm uninstall <version> uninstalls a specific version
- nvm use <version> uses a specific version.
- nvm alias default <version> makes a particular version the default for your system.
- nvm ls-remote view the list of available Node.js versions that you can install.

Having multiple versions of Node.js installed on your system can help you avoid compatibility issues and ensure that your applications or projects run smoothly on different environments.

Top comments (0)