Skip to content

Install npm with fnm

npm (Node Package Manager) is installed along with Node.js. This guide introduces how to manage npm through fnm.

Relationship Between npm and fnm

When you install Node.js using fnm, npm is automatically included:

bash
# Install Node.js (automatically includes npm)
fnm install 20

# Verify npm version
npm --version

Check npm Version

bash
# View current npm version
npm --version

# View Node.js and npm versions
node --version
npm --version

Different Node.js versions correspond to different npm versions:

Node.jsnpm
20.x10.x
18.x9.x
16.x8.x

Upgrade npm

Using npm Self-Upgrade

bash
# Upgrade to latest version
npm install -g npm@latest

# Upgrade to specific version
npm install -g npm@10.2.0

Switch Node.js Version

Switching Node.js version also switches npm version:

bash
# Switch to Node.js 20
fnm use 20
npm --version  # Output: 10.x.x

# Switch to Node.js 18
fnm use 18
npm --version  # Output: 9.x.x

npm Global Package Management

View Global Installation Path

bash
npm config get prefix

Global Package Location

Node.js global packages managed by fnm are located at:

~/.fnm/node-versions/<version>/installation/lib/node_modules

Install Global Packages

bash
# Install global packages
npm install -g pnpm
npm install -g yarn
npm install -g typescript

# View global packages
npm list -g --depth=0

Use Corepack

Corepack is a package manager manager for Node.js that can manage npm, yarn, and pnpm:

Enable Corepack

bash
# Enable Corepack when installing Node.js
fnm install 20 --corepack-enabled

# Or set environment variable
export FNM_COREPACK_ENABLED=true
fnm install 20

Use Corepack

bash
# Enable Corepack
corepack enable

# Use specific version of yarn
corepack prepare yarn@stable --activate

# Use specific version of pnpm
corepack prepare pnpm@latest --activate

npm Configuration

Configure Mirror

bash
# Set npm mirror
npm config set registry https://registry.npmmirror.com

# View current configuration
npm config list

# Restore default mirror
npm config set registry https://registry.npmjs.org

Common Mirrors

Mirror SourceURL
npm Officialhttps://registry.npmjs.org
npmmirrorhttps://registry.npmmirror.com
Tencent Cloudhttps://mirrors.cloud.tencent.com/npm/

Multi-Version npm Management

Since npm is installed with Node.js, the best way to manage multiple npm versions is:

Method 1: Switch Node.js Version

bash
# Install multiple Node.js versions
fnm install 20
fnm install 18

# Switch versions
fnm use 20  # npm 10.x
fnm use 18  # npm 9.x

Method 2: Upgrade npm Independently

bash
# Upgrade npm in current Node.js version
npm install -g npm@latest

Common Issues

npm Command Not Found

bash
# Confirm Node.js is installed
fnm list

# Confirm currently used version
fnm current

# Reinstall Node.js
fnm install 20 --use

Global Package Installation Failed

bash
# Clear npm cache
npm cache clean --force

# Check permissions
ls -la ~/.fnm/node-versions/

# Reinstall
npm install -g <package>

npm Version Mismatch

bash
# Check Node.js version
fnm current

# Reset npm to default version
fnm uninstall <version>
fnm install <version>

Best Practices

1. Use package.json engines

Specify Node.js and npm versions in the project:

json
{
  "engines": {
    "node": ">=18.0.0",
    "npm": ">=9.0.0"
  }
}

2. Use .npmrc File

Create .npmrc file in the project:

registry=https://registry.npmmirror.com
save-exact=true

3. Lock Package Manager

Use packageManager field:

json
{
  "packageManager": "npm@10.2.0"
}