Skip to content

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 --lts

Install Latest Version

bash
fnm install --latest

Install Specific Version

bash
fnm install 20
fnm install 18.17.0

Installation 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.x

LTS Version Codenames:

CodenameVersionStatus
Iron20.xActive LTS
Hydrogen18.xMaintenance
Gallium16.xEnd 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.3

Install 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 install

Use 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-missing

Set Default Version

bash
# Set default version
fnm default 20

# View current default version
fnm default

Use Immediately After Installation

bash
# Install and switch to the version immediately
fnm install 20 --use

View 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 --latest

View Locally Installed Versions

bash
fnm list

Output example:

* v20.10.0 default
  v18.19.0
  v16.20.2

Use 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/node

Common mirrors:

Mirror SourceURL
npmmirrorhttps://npmmirror.com/mirrors/node
Tsinghua Universityhttps://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 20

Version 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-version

Combined 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 18

Use Aliases

bash
# Set aliases
fnm alias 20 default
fnm alias 18 legacy

# Switch using aliases
fnm use default
fnm use legacy

Common Issues

Installation Failed

If installation fails, please check:

  1. Is network connection normal
  2. Do you need to configure a mirror
  3. Is there enough disk space
bash
# Retry using mirror
fnm install 20 --node-dist-mirror=https://npmmirror.com/mirrors/node

Version Switch Not Working

Ensure Shell is configured correctly:

bash
# Check current version
fnm current

# Check node path
which node

Clean Up Old Versions

bash
# View installed versions
fnm list

# Uninstall unneeded versions
fnm uninstall 16.20.2