Back to Blog
Lesson 2 of the Node.js: Build Your First Server & CLI course
Node.jsJuly 13, 20264 min read

Setting Up Your Environment with NVM

Master NVM to manage Node.js versions like a pro. Learn to install, switch, and verify your Node.js environment for consistent, error-free backend development.

Node.jsNVMCLIEnvironment SetupVersion Management

Previously in this course, we explored Understanding the Node.js Architecture to see how the engine processes code. Now that you understand the "why" behind the runtime, it's time to set up the "how."

If you install Node.js directly from the official website, you’ll likely end up with one global version. This is a common trap: different projects often require different versions of Node.js. If you upgrade to test a new feature, you might accidentally break an older project relying on an outdated API.

The industry-standard solution to this problem is NVM (Node Version Manager). It allows you to install multiple versions of Node.js side-by-side and switch between them with a single command.

What is NVM and Why Use It?

NVM is a CLI tool that manages your Node.js installation directory. Instead of the system-wide installation, NVM keeps versions isolated in your user folder.

FeatureGlobal Node.js InstallationNVM Managed
Version ControlSingle version onlyMultiple versions
PermissionsOften requires sudoNo sudo required
FlexibilityDifficult to downgradeInstant switching
SafetyRisk of breaking system toolsIsolated environment

Installing NVM

To install NVM, you need to run the installation script from their official repository. Open your terminal and use curl or wget.

For macOS and Linux

Run the following command in your terminal:

Bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

After the script completes, close your terminal and reopen it, or run source ~/.bashrc (or source ~/.zshrc if you use Zsh). Verify the installation by typing:

Bash
nvm --version

If you see a version number, you're ready to go.

For Windows

The official NVM script is for Unix-based systems. For Windows, download the nvm-windows installer. It provides a similar CLI experience but handles Windows-specific paths.

Managing Node.js Versions

Once NVM is installed, you don't need to download Node.js from the website anymore. You manage everything through the nvm command.

Installing a Version

To install the latest Long Term Support (LTS) version of Node.js:

Bash
nvm install --lts

If you need a specific version (e.g., v18.16.0):

Bash
nvm install 18.16.0

Switching Versions

You can switch between installed versions instantly. To use a specific version in your current terminal session:

Bash
nvm use 18.16.0

Verifying Installation

Always verify your current version after switching to ensure your terminal is pointing to the right environment:

Bash
node -v

If you see the expected version number, your environment is correctly configured.

Hands-on Exercise

  1. Use nvm install 16.0.0 to install an older version.
  2. Use nvm install --lts to install the newest stable version.
  3. List your installed versions using nvm ls.
  4. Switch between them using nvm use and verify the version with node -v each time.

Common Pitfalls

  • "Command not found": This usually happens if you haven't sourced your shell profile after installation. Ensure your .bashrc or .zshrc contains the NVM export lines.
  • Forgetting to use nvm use: NVM does not automatically switch versions when you enter a directory unless you use an .nvmrc file (which we will cover later). Always check your node -v if things act strangely.
  • Mixing Global and NVM: If you previously installed Node via a package manager like brew or apt, uninstall those global versions to avoid conflicts with NVM.

FAQ

Do I need to reinstall my global packages every time I switch versions? Yes. Global packages (like nodemon or typescript) are installed specifically for the active Node version. Use nvm install --lts --reinstall-packages-from=current to migrate them.

Is NVM the only way to manage versions? No, tools like fnm (Fast Node Manager) or volta are popular alternatives, but NVM remains the most widely used and documented tool in the ecosystem.

Recap

You’ve now installed NVM, learned how to manage multiple Node.js versions, and verified your setup. This isolation is critical for building a stable REST API, as it ensures your project works exactly the same way on your machine as it will on your production server.

Up next: We will dive into the REPL and learn how to run your first JavaScript files.

Similar Posts