Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NVM Managing Multiple Versions of Node.js

NVM Managing Multiple Versions of Node.js

nvm (Node Version Manager) is a very useful tool that lets you install and manage multiple versions of Node.js on the same machine.

Why do you need nvm?

Installing nvm

Installing nvm on macOS/Linux:



# Install using curl

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



# or use wget to install

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash



# Reload shell configuration

source ~/.bashrc

# 或

source ~/.zshrc

Installing nvm-windows on Windows:

  1. Download nvm-windows:https://github.com/coreybutler/nvm-windows/releases
  2. Download nvm-setup.zip
  3. Extract and run the installer

Common nvm commands:



# Check nvm version

nvm --version



# List all installable Node.js versions

nvm list-remote

# Used on Windows

nvm list available



# Install the latest LTS version

nvm install --lts



# Install specific version

nvm install 18.17.0

nvm install 16.20.1



# List installed versions

nvm list

# 或

nvm ls



# Switch to a specific version

nvm use 18.17.0



# Set default version

nvm alias default 18.17.0



# Check the current version being used

nvm current



# Uninstall specific version

nvm uninstall 16.20.1

Practical usage examples:



# Scenario: Use different Node.js versions for different projects



# Project A uses Node.js 18

cd project-a

nvm use 18.17.0

node --version  # v18.17.0



# Project B uses Node.js 16

cd ../project-b

nvm use 16.20.1

node --version  # v16.20.1



# Specify Node.js version for the project

echo "18.17.0" > .nvmrc

nvm use  # 自动使用 .nvmrc 中指定的版本

to verify the installation succeeded

Create your first Node.js program:

Create an environment named hello.js :

Example

// hello.js

console.

log

(

'Hello, Node.js!'

)

;

console.

log

(

'Node.js Version:'

,

process.

version

)

;

console.

log

(

'Current Working Directory:'

,

process.

cwd

(

)

)

;

console.

log

(

'Operating System:'

,

process.

platform

)

;

Expected output:



Hello, Node.js!

Node.js 版本: v18.17.0

当前工作目录: /Users/username/projects

操作系统: darwin

Check the global installation path:



# View npm global package installation path

npm config get prefix



# View npm configuration

npm config list



# Check Node.js installation path

which node

# Used on Windows

where node