Bratish Goswami

Personal website and blog

Fixing Node.js and npm Installation Issues on Ubuntu

Been struggling with Node.js and npm installation on Ubuntu today. The issue was confusing at first - I could install nodejs but npm kept failing with some weird errors.

The Problem

When I tried to install packages with npm, I kept getting errors like:

/usr/bin/env: node: No such file or directory

But nodejs was clearly installed! I could run nodejs --version and it worked fine.

The Solution

Turns out this is a common issue with Ubuntu's package naming. Ubuntu renames the Node.js binary to nodejs to avoid conflicts with another package called node. But npm and many Node.js tools expect the binary to be called node.

The fix is simple:

sudo apt-get install nodejs-legacy

This package creates a symlink from node to nodejs, which solves the compatibility issue.

Alternative Solutions

If you don't want to install the legacy package, you can create the symlink manually:

sudo ln -s /usr/bin/nodejs /usr/bin/node

Or if you're using a more recent version of Ubuntu, you might need:

sudo apt-get install nodejs npm

Why This Happens

The Debian maintainers had to make this decision because there was a namespace collision with another package. It's one of those things that makes sense from a packaging perspective but can be confusing for developers.

Testing the Fix

After installing nodejs-legacy, both of these should work:

node --version
npm --version

Hope this helps someone else who runs into the same issue!