Home >Web Front-end >JS Tutorial >Installing and Managing Node.js with NVM

Installing and Managing Node.js with NVM

DDD
DDDOriginal
2025-01-20 16:51:10679browse

Installing and Managing Node.js with NVM

In 2025, Node.js will still be an indispensable tool for JavaScript development. This guide not only covers installing Node.js on different platforms, but also provides in-depth information on how to use the Node Version Manager (NVM) to efficiently manage multiple Node.js versions.

What is NVM?

NVM (Node Version Manager) is a command line tool that allows developers to easily manage multiple Node.js versions.

Key features of NVM:

  • Allows installation, switching and removal of different Node.js versions on one machine.
  • Provides a simple terminal command interface to manage Node.js versions.
  • Allows multiple Node.js versions to run on the same computer.
  • Conveniently switch Node.js versions between different projects to avoid compatibility issues.
  • Also manages npm (Node Package Manager) installations corresponding to each Node.js version.
  • Supports Unix, macOS and Windows (via WSL).

NVM is especially useful for projects that require a specific Node.js version, allowing seamless switching between versions and avoiding version conflicts.

Alternative: If you prefer the traditional installation method, you can install it directly from the Node.js official website (https://www.php.cn/link/beddf554eb637cbe8c079b879c79c29b.

Install NVM

On macOS and Linux: Open a terminal and run the installation command:

<code>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash</code>

Update shell configuration file:

<code>source ~/.bashrc # 对于bash
source ~/.zshrc  # 对于zsh</code>

Verify installation:

<code>nvm --version</code>

On Windows: Download the nvm-windows installer from GitHub, run the installer and follow the instructions.

Open a new command prompt and verify installation:

<code>nvm version</code>

Install Node.js using NVM

After installing NVM, you can easily manage different Node.js versions.

Install the latest LTS version:

<code>nvm install --lts</code>

Install a specific version:

<code>nvm install 16.14.0</code>

List installed versions:

<code>nvm ls</code>

Switch between versions:

<code>nvm use 16.14.0</code>

Advanced configuration of the project

To ensure version consistency of the team project, create a .nvmrc file in the project root directory:

<code>16.14.0</code>

Any team member can then simply run:

<code>nvm use</code>

Tips and Best Practices

For production projects, always use LTS versions. Update NVM to the latest version regularly. Implement .nvmrc files across all projects to maintain team consistency. Back up the global npm configuration before switching versions. Use npm scripts to automate version switching across environments.

Advanced features of NVM

Custom alias:

<code>nvm alias myproject 14.17.0
nvm use myproject</code>

Run the command with a specific version:

<code>nvm exec 14.17.0 node script.js</code>

Install global packages by version:

<code>nvm use 14.17.0
npm install -g yarn</code>

Common Troubleshooting

PATH conflict: check your shell profile configuration. Permissions on macOS/Linux:

<code>sudo chown -R $(whoami) ~/.nvm</code>

Performance on Windows: Consider using Windows Subsystem for Linux (WSL) for a Unix-like experience.

Conclusion

In 2025, efficiently using NVM to manage Node.js installations is more important than ever. With the JavaScript ecosystem rapidly evolving, being able to easily switch between Node.js versions and manage multiple development environments is crucial for modern developers. By mastering NVM and following the best practices outlined in this guide, you'll be well-equipped to handle the challenges of contemporary Node.js development, ensuring your project's flexibility and efficiency.

Original article published at https://codeinit.dev/blog/instalacao-e-gerenciamento-do-nodejs-com-nvm

The above is the detailed content of Installing and Managing Node.js with NVM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn