DEV Community

Cover image for Upgrading Node.js on Linux using NVM
Dessad
Dessad

Posted on

Upgrading Node.js on Linux using NVM

If you're trying to upgrade Node.js from version 12 to a more recent version (e.g., 18 LTS) on a Linux distribution like Parrot OS and encountering issues along the way, this guide is for you. It will cover each step in detail, including common problems and solutions.

Prerequisites

Before you begin, make sure you have the following:

  • A Linux distribution (e.g., Parrot OS)
  • An existing installation of Node.js version 12
  • Administrative access to your system

1. Installing NVM

Check your package manager:
Update your package manager to ensure you have the latest package information.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install NVM (Node Version Manager):
NVM will allow you to easily switch between Node.js versions.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh
Enter fullscreen mode Exit fullscreen mode
source ~/.bashrc # Source NVM in your current session
Enter fullscreen mode Exit fullscreen mode

List available Node.js versions:
See the available Node.js versions for installation.

nvm ls-remote
Enter fullscreen mode Exit fullscreen mode

2. Upgrading Node.js

Install Node.js version 18:
Replace 18 with the version you want.

nvm install 18
Enter fullscreen mode Exit fullscreen mode

Set Node.js 18 as the default version:
To set it as the default:

nvm alias default 18
Enter fullscreen mode Exit fullscreen mode

Or, for the current session:

nvm use 18
Enter fullscreen mode Exit fullscreen mode

Update npm (Node Package Manager):

npm install -g npm@latest
Enter fullscreen mode Exit fullscreen mode

3. Troubleshooting Common Issues

NVM Command Not Found

If you encounter the "nvm command not found" error:

  • Source NVM in your shell profile (e.g., ~/.bashrc or ~/.zshrc) or use the correct shell-specific configuration file.

Node.js Version Rolls Back to 12

If Node.js version rolls back to 12 after closing the terminal:

  • Check that the NVM sourcing commands are present and correctly placed in your shell profile.
  • Ensure the shell profile is sourced when opening a new terminal session.

Issues with .bashrc

If you face issues with ~/.bashrc:

  • Create the ~/.bashrc file if it doesn't exist.
touch ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Add the NVM sourcing commands to ~/.bashrc.
nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Source ~/.bashrc to apply changes to your current session.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
Enter fullscreen mode Exit fullscreen mode
  • After you've saved the ~/.bashrc file, you need to source it to apply the changes to your current terminal session.
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Verify NVM installation and set the default Node.js version.
nvm --version
Enter fullscreen mode Exit fullscreen mode

These steps should help you upgrade Node.js and address common problems that may arise during the process. Remember to test your Node.js projects to ensure compatibility with the new version.

Top comments (0)