How to Resolve “nodejs Conflicts with npm” Dependency Error on Ubuntu/Linux


1 views

The error occurs because Ubuntu's official repositories contain outdated versions of Node.js and npm that have dependency conflicts. When you try to install both packages simultaneously using apt-get, the package manager detects version incompatibilities.

For modern development, I recommend using either NodeSource or nvm for installation:

Method 1: Using NodeSource

# For Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

Method 2: Using nvm (Node Version Manager)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# Restart terminal
nvm install --lts
nvm use --lts

If you've already attempted installations, run these cleanup commands:

sudo apt-get purge nodejs npm
sudo rm -rf /usr/local/bin/npm /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules/
sudo apt-get autoremove

After successful installation, verify with:

node --version
npm --version

If npm commands aren't found after installation:

export PATH="$PATH:/usr/local/bin"
# Add to your .bashrc or .zshrc for persistence
echo 'export PATH="$PATH:/usr/local/bin"' >> ~/.bashrc

For projects that don't specifically require npm:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn

If you must use specific older versions:

nvm install 0.10.15
nvm use 0.10.15
npm install -g npm@2.15.1

When working with Ubuntu's package manager (apt), you might encounter frustrating dependency issues between Node.js and npm. The core problem stems from:

  • Ubuntu's repositories often contain outdated versions
  • Conflicts between the system-installed npm and Node.js versions
  • Mixed installation methods causing version mismatches

First, completely remove existing installations:

sudo apt-get purge --auto-remove nodejs npm
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules

For Ubuntu 13.04+, use NodeSource repositories for stable versions:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

This handles both Node.js and npm installation automatically with proper version alignment.

For version management flexibility, Node Version Manager is ideal:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node  # Installs latest stable
nvm use node

After successful installation, verify both components:

node -v  # Should return v14.x.x or higher
npm -v   # Should return corresponding npm version

If problems persist, manually link binaries:

sudo ln -s /usr/bin/nodejs /usr/bin/node
npm config set prefix ~/.npm-global
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
  • Never mix installation methods (apt vs source vs nvm)
  • Always clean previous installations completely
  • Consider using --force only as last resort
  • For production environments, pin specific versions