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 problem…


This content originally appeared on DEV Community and was authored by Dessad

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

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
source ~/.bashrc # Source NVM in your current session

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

nvm ls-remote

2. Upgrading Node.js

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

nvm install 18

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

nvm alias default 18

Or, for the current session:

nvm use 18

Update npm (Node Package Manager):

npm install -g npm@latest

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
  • Add the NVM sourcing commands to ~/.bashrc.
nano ~/.bashrc
  • 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
  • After you've saved the ~/.bashrc file, you need to source it to apply the changes to your current terminal session.
source ~/.bashrc
  • Verify NVM installation and set the default Node.js version.
nvm --version

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.


This content originally appeared on DEV Community and was authored by Dessad


Print Share Comment Cite Upload Translate Updates
APA

Dessad | Sciencx (2024-10-20T15:09:30+00:00) Upgrading Node.js on Linux using NVM. Retrieved from https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/

MLA
" » Upgrading Node.js on Linux using NVM." Dessad | Sciencx - Sunday October 20, 2024, https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/
HARVARD
Dessad | Sciencx Sunday October 20, 2024 » Upgrading Node.js on Linux using NVM., viewed ,<https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/>
VANCOUVER
Dessad | Sciencx - » Upgrading Node.js on Linux using NVM. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/
CHICAGO
" » Upgrading Node.js on Linux using NVM." Dessad | Sciencx - Accessed . https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/
IEEE
" » Upgrading Node.js on Linux using NVM." Dessad | Sciencx [Online]. Available: https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/. [Accessed: ]
rf:citation
» Upgrading Node.js on Linux using NVM | Dessad | Sciencx | https://www.scien.cx/2024/10/20/upgrading-node-js-on-linux-using-nvm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.