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 --versionCheck npm Version
bash
# View current npm version
npm --version
# View Node.js and npm versions
node --version
npm --versionDifferent Node.js versions correspond to different npm versions:
| Node.js | npm |
|---|---|
| 20.x | 10.x |
| 18.x | 9.x |
| 16.x | 8.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.0Switch 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.xnpm Global Package Management
View Global Installation Path
bash
npm config get prefixGlobal Package Location
Node.js global packages managed by fnm are located at:
~/.fnm/node-versions/<version>/installation/lib/node_modulesInstall 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=0Use 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 20Use 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 --activatenpm 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.orgCommon Mirrors
| Mirror Source | URL |
|---|---|
| npm Official | https://registry.npmjs.org |
| npmmirror | https://registry.npmmirror.com |
| Tencent Cloud | https://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.xMethod 2: Upgrade npm Independently
bash
# Upgrade npm in current Node.js version
npm install -g npm@latestCommon 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 --useGlobal 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=true3. Lock Package Manager
Use packageManager field:
json
{
"packageManager": "npm@10.2.0"
}Related Links
- Install Node.js - Install Node.js
- Version Management - Version management guide
- Command Reference - All command details