Ubuntu系统下如何安装npm与Node.js?安装Node.js后npm异常怎么办?
Hey there, let's work through this Node.js and npm issue on Ubuntu together. I’ve helped plenty of developers troubleshoot exactly this kind of problem, so let’s break it down into actionable steps.
1. First: The Recommended Way to Install Node.js + npm (Avoids Old System Packages)
Ubuntu’s default repo often has outdated Node.js versions, which can cause compatibility headaches with npm. The most reliable approach is using the NodeSource PPA:
- Start by updating your system’s package index and upgrading existing packages:
sudo apt update && sudo apt upgrade -y - Install required dependencies for adding the PPA:
sudo apt install -y curl gnupg2 ca-certificates lsb-release - Add the NodeSource PPA (we’ll use the LTS 20.x branch here—swap
20.xfor18.xif you need an older LTS):curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list - Update the index again and install Node.js (npm is included automatically):
sudo apt update && sudo apt install -y nodejs - Verify the installation works:
node --version npm --version
2. Fixing npm Issues When Node.js Is Already Installed
If you already have Node.js set up but npm is throwing errors or npm --version isn’t working, try these troubleshooting steps:
Method 1: Reinstall npm from Scratch
Corrupted npm files are a common culprit. Let’s wipe the existing npm and reinstall it:
# Uninstall the system-level npm sudo apt remove --purge -y npm # Clean up leftover files that might cause conflicts sudo rm -rf /usr/local/lib/node_modules /usr/local/bin/npm /usr/local/bin/npx # Reinstall Node.js (which brings a fresh copy of npm) sudo apt install --reinstall -y nodejs
Run npm --version again to check if this fixes the issue.
Method 2: Use nvm for Version Management (Avoids Permission & Conflict Issues)
If system-level installs keep causing problems, nvm (Node Version Manager) is a game-changer—it lets you manage multiple Node.js/npm versions without root permissions:
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - Restart your terminal, or run this command to load nvm immediately:
source ~/.bashrc # For zsh users: source ~/.zshrc - Install your preferred Node.js version (e.g., LTS 20.x):
nvm install 20 - Set it as your default version:
nvm alias default 20 - Verify everything works:
node --version npm --version
Method 3: Fix npm Permission Issues
Sometimes npm fails because it doesn’t have access to its installation directories. Let’s check and fix that:
- First, find npm’s global prefix path:
npm config get prefix - If the path is
/usr/local, fix the permissions so your user owns the directories:sudo chown -R $USER:$GROUP /usr/local/lib/node_modules sudo chown -R $USER:$GROUP /usr/local/bin - Then update npm to the latest version:
npm install -g npm@latest
内容的提问来源于stack exchange,提问作者godot

