Problem Description
When running npm install on Ubuntu 16.04, you may encounter an error indicating the Node.js version is too low. A typical error message is: wanted: node >=6 current: node 4.2.6 npm 3.5.2. This shows the system's installed Node.js version (4.2.6) is lower than the project's minimum required version (6.x).
Root Cause
The default Ubuntu 16.04 software repositories contain an outdated Node.js version (e.g., v4.2.6), which cannot meet the requirements of modern npm packages or projects.
Standard Upgrade Methods
It is recommended to use the official NodeSource repository or the Node version manager n to install a newer Node.js version.
Method 1: Use the NodeSource Repository (Recommended)
This method installs the latest LTS or Current version directly.
# 1. Remove old versions (optional)
sudo apt-get remove --purge nodejs npm
# 2. Add NodeSource repository (e.g., for Node.js 18 LTS)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# 3. Install Node.js and npm
sudo apt-get install -y nodejs
# 4. Verify installation
node --version
npm --version
Method 2: Use the Node Version Manager n
Use n if you need to switch between multiple Node.js versions.
# 1. First, install a recent Node.js version (e.g., via NodeSource as in Method 1).
# 2. Install n globally
sudo npm install -g n
# 3. Use n to install a specific Node.js version
# Install latest LTS
sudo n lts
# Or install latest stable
sudo n latest
# Or install a specific version like 16.x
sudo n 16
# 4. Verify
node --version
Fixing Syntax Errors During Upgrade
When running commands like sudo n stable, you might encounter an error:
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136
Cause: This error indicates the Node.js version currently running the n command is too old (e.g., v4.x) and cannot parse the ES6 syntax (like let, const) in the n tool's source code.
Solution: Bypass the old Node.js by directly fetching and running a newer n binary.
# 1. Clean up any old n installation
sudo npm uninstall -g n
sudo rm -rf /usr/local/n
sudo rm -rf /usr/local/bin/n
# 2. Use curl to download and install n directly
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
# Install latest LTS version
sudo bash n lts
# 3. Now install n globally via npm (Node.js is now updated)
sudo npm install -g n
# 4. Use n to switch versions
sudo n lts
# or
sudo n latest
# 5. Verify
node --version
which node
Summary and Recommendations
- For Ubuntu 16.04: The default Node.js version is severely outdated. Do not use
apt-get install nodejs-legacy. - Preferred Method: Use the NodeSource repository (Method 1) to install a recent LTS version (e.g., 18.x). This is the most straightforward and reliable approach.
- Multi-Version Management: If you need to manage multiple projects or versions, first install a base version via NodeSource, then use
nornvmfor switching. - Path Issues: If the
nodecommand is not found after installation, try logging out and back in, or runhash -rto refresh your shell's command cache.
Following these steps should successfully upgrade Node.js and npm on Ubuntu 16.04 to a version that meets your project's requirements.