How to Change Node Version using NVM and Node

Changing the Node version on your system typically involves using a version manager. This allows you to switch between different versions for various projects. Below are the steps using two popular version managers: Node Version Manager (NVM) and Node (n). Before proceeding, ensure you have command-line/terminal access.

Using Node Version Manager (NVM)

1. Install NVM:

  • Open your terminal.
  • Install NVM by running the install script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  • Restart your terminal or run source ~/.nvm/nvm.sh to start using it immediately.

2. Install the Node version you need:

  • List all available versions:
nvm list-remote
  • Install a specific version of Node:
nvm install 14.17.0  # replace with your preferred version

3. Switch between installed Node versions:

  • Use the following command to switch:
nvm use 14.17.0  # replace with the version you want to use

4. Set a default Node version:

Set a version to default so it’s used in any new shell:

nvm alias default 14.17.0  # replace with your preferred version

Using Node (n)

1. Install Node (n):

  • Node (n) can be installed via npm:
npm install -g n

2. Change Node version:

Install and use the latest version:

n latest

Or install and use a specific version:

n 14.17.0

3. Switch between versions:

  • Once installed, switch between the installed versions using:
n
  • Then select the version you want to switch to using the arrow keys and Enter.

Tips and Troubleshooting:

  • Restart your terminal: After installing a new Node version, you may need to open a new terminal window or tab to use it.
  • Check the version: You can always check the current Node version by running node -v.
  • Permissions: If you encounter permissions errors, you might need to run the install commands with sudo (for macOS/Linux) or run your command prompt as an administrator (for Windows).
  • Compatibility: Ensure any global npm packages you have are compatible with the new Node version, or reinstall them as needed.