Home >Web Front-end >JS Tutorial >Installing Multiple Versions of Node.js Using nvm
Managing Multiple Node.js Versions with NVM
Working on multiple Node.js projects often requires juggling different runtime versions. This article explores how Node Version Manager (NVM) simplifies this process, allowing seamless switching between various Node.js versions without the hassle of manual installations and uninstalls.
Key Advantages of NVM:
Understanding NVM:
NVM (Node Version Manager) is a command-line tool that simplifies the management of multiple Node.js installations. It provides commands to install, switch between, and manage different Node.js versions.
Operating System Support:
NVM directly supports Linux and macOS. For Windows, use nvm-windows (a separate, but similar, project). The core commands outlined below generally work across both NVM and nvm-windows.
Installation:
Windows:
C:Program Filesnodejs
).C:Users<user>AppDataRoamingnpm</user>
).macOS/Linux:
Removing prior Node.js and npm installations is optional but recommended. Numerous online resources guide you through this process.
Install NVM using cURL or wget:
cURL:
<code class="language-bash">curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>
wget:
<code class="language-bash">wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>
(Note: The version number (v0.35.2) might change; check the NVM project page for the latest version.)
After installation, reload or restart your terminal for NVM to take effect.
Using NVM:
Once installed, the nvm
command becomes available in your terminal.
Installing Multiple Node.js Versions:
Use nvm install
followed by the version number:
<code class="language-bash">curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>
This installs Node.js version 12.14.1. NVM uses semantic versioning (SemVer); nvm install 12.14
installs the latest 12.14.x version. nvm ls-remote
(or nvm ls available
for nvm-windows) lists available versions.
Installing npm:
NVM installs a compatible npm version with each Node.js installation. Global npm packages are installed per Node.js version in ~/.nvm/versions/node/<version>/lib/node_modules</version>
, preventing conflicts.
Switching Between Versions:
Use nvm use
followed by the version number:
<code class="language-bash">wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash</code>
This switches to Node.js 13.6.0. nvm use node
selects the latest version, and nvm use --lts
selects the latest LTS (Long-Term Support) release.
Custom Aliases:
Create custom aliases using nvm alias
:
<code class="language-bash">nvm install 12.14.1</code>
Set a default version with nvm alias default <version></version>
.
Listing Installed Versions:
Use nvm ls
to list installed versions. The currently active version is marked with an arrow. nvm current
shows the active version.
Per-Project Node Version Specification:
Create a .nvmrc
file in a project directory containing the desired Node.js version. NVM automatically detects and uses this version when you cd
into the project directory and run nvm use
. (Optional shell configuration may be needed for automatic activation).
Other NVM Commands:
nvm run <version> <command></command></version>
: Runs a command using a specific Node.js version without switching.nvm exec <version> <command></command></version>
: Runs a command in a subshell with a specific Node.js version.nvm which <version></version>
: Gets the path to the Node.js executable for a specific version.Conclusion:
NVM is an invaluable tool for Node.js developers, simplifying version management and saving significant time and effort.
FAQs (briefly addressed within the main text):
nvm update
nvm install <version></version>
, nvm uninstall <version></version>
.nvm ls
nvm alias default <version></version>
nvm use <version></version>
The above is the detailed content of Installing Multiple Versions of Node.js Using nvm. For more information, please follow other related articles on the PHP Chinese website!