Environment Setup and nvm Version Management
To build robust server-side applications, you need a stable Node.js runtime. Since different projects may require different Node.js versions, installing version managers like Node Version Manager (nvm) is highly recommended.
1. What is Node Version Manager (nvm)?
Node Version Manager (nvm) is a command-line tool that allows you to install, manage, and switch between multiple versions of Node.js on a single machine.
Key benefits include:
- Running older projects on legacy Node.js versions without conflict.
- Testing new applications on bleeding-edge Node.js runtimes.
- Installing Node.js without superuser or root permissions, preventing permission errors when installing global NPM packages.
2. Installing nvm
On macOS and Linux
You can install nvm using curl or wget. Run this command in your terminal:
# Install nvm using curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashAfter installation, reload your shell configuration (for example, run source ~/.bashrc or source ~/.zshrc) to activate the command-line helper.
On Windows
For Windows systems, use the standalone utility nvm-windows.
- Download the latest installer from the official repository release page.
- Run the installer and follow the wizard instructions.
- Open a new Command Prompt or PowerShell window to verify the installation.
3. Essential nvm Commands
Use the following CLI commands to manage your runtimes:
# Check installed nvm version
nvm --version
# List all available Node.js LTS versions for installation
nvm list-remote
# Install a specific version of Node.js
nvm install 22.19.0
# Install the latest Long Term Support (LTS) release
nvm install --lts
# List all locally installed versions
nvm list
# Switch to a specific installed version
nvm use 22.19.0
# Set a default Node.js version for new terminals
nvm alias default 22.19.04. Verifying the Installation
Once Node.js is installed and activated, you can verify that both the runtime and package manager are working properly:
# Check the Node.js version
node -v
# Check the npm version
npm -v5. Working with .nvmrc Files
To enforce a specific Node.js version for a project, you can place a file named .nvmrc at the root of your project directory. This file should contain only the version number.
Example content of .nvmrc:
22.19.0Team members can then switch to the correct version simply by running:
# Switch to the version defined in .nvmrc
nvm use