Install Node.js with fnm
fnm makes installing and managing Node.js versions simple and fast. This guide will detail how to use fnm to install Node.js.
Quick Start
Install Latest LTS Version
bash
fnm install --ltsInstall Latest Version
bash
fnm install --latestInstall Specific Version
bash
fnm install 20
fnm install 18.17.0Installation Methods Explained
Install LTS Version
Node.js LTS (Long Term Support) versions are recommended for production environments:
bash
# Install latest LTS version
fnm install --lts
# Install specific LTS version
fnm install lts/iron # Node.js 20.x
fnm install lts/hydrogen # Node.js 18.x
fnm install lts/gallium # Node.js 16.xLTS Version Codenames:
| Codename | Version | Status |
|---|---|---|
| Iron | 20.x | Active LTS |
| Hydrogen | 18.x | Maintenance |
| Gallium | 16.x | End of Life |
Install Specific Version
bash
# Install major version (automatically selects latest minor version)
fnm install 20
# Install specific version
fnm install 20.10.0
# Install older version
fnm install 14.21.3Install Using Version File
Create a .node-version or .nvmrc file in the project directory:
bash
# Create .node-version file
echo "20" > .node-version
# fnm will automatically read and install
fnm installUse After Installation
Switch to Installed Version
bash
# Use specific version
fnm use 20
# If version is not installed, auto install then use
fnm use 18 --install-if-missingSet Default Version
bash
# Set default version
fnm default 20
# View current default version
fnm defaultUse Immediately After Installation
bash
# Install and switch to the version immediately
fnm install 20 --useView Available Versions
View Remote Versions
bash
# List all available versions
fnm list-remote
# List only LTS versions
fnm list-remote --lts
# Filter specific versions
fnm list-remote --filter=18
# View latest versions
fnm list-remote --latestView Locally Installed Versions
bash
fnm listOutput example:
* v20.10.0 default
v18.19.0
v16.20.2Use Mirror for Faster Download
In mainland China, you can use a mirror to accelerate downloads:
bash
# Set mirror environment variable
export FNM_NODE_DIST_MIRROR=https://npmmirror.com/mirrors/node
# Or specify during installation
fnm install 20 --node-dist-mirror=https://npmmirror.com/mirrors/nodeCommon mirrors:
| Mirror Source | URL |
|---|---|
| npmmirror | https://npmmirror.com/mirrors/node |
| Tsinghua University | https://mirrors.tuna.tsinghua.edu.cn/nodejs-release |
Corepack Support
fnm supports Corepack, which can automatically enable Yarn and pnpm:
bash
# Enable Corepack during installation
fnm install 20 --corepack-enabled
# Or set environment variable
export FNM_COREPACK_ENABLED=true
fnm install 20Version Management Best Practices
Project-Level Version Management
Create a .node-version file in the project root:
bash
# Specify major version
echo "20" > .node-version
# Or specify exact version
echo "20.10.0" > .node-versionCombined with --use-on-cd option, automatically switch versions when entering the directory:
bash
# Add to Shell configuration
eval "$(fnm env --use-on-cd)"Multi-Version Development
bash
# Install multiple versions
fnm install 20
fnm install 18
fnm install 16
# Switch between different projects
cd project-a && fnm use 20
cd project-b && fnm use 18Use Aliases
bash
# Set aliases
fnm alias 20 default
fnm alias 18 legacy
# Switch using aliases
fnm use default
fnm use legacyCommon Issues
Installation Failed
If installation fails, please check:
- Is network connection normal
- Do you need to configure a mirror
- Is there enough disk space
bash
# Retry using mirror
fnm install 20 --node-dist-mirror=https://npmmirror.com/mirrors/nodeVersion Switch Not Working
Ensure Shell is configured correctly:
bash
# Check current version
fnm current
# Check node path
which nodeClean Up Old Versions
bash
# View installed versions
fnm list
# Uninstall unneeded versions
fnm uninstall 16.20.2Related Links
- Install fnm - Install fnm
- Command Reference - All command details
- Version Management - Detailed version management guide