Explain Codes LogoExplain Codes Logo

Nodejs vs node on ubuntu 12.04

javascript
nodejs
ubuntu
nvm
Nikita BarsukovbyNikita Barsukov·Oct 30, 2024
TLDR

To install Node.js on Ubuntu, run:

sudo apt-get install nodejs

Due to a naming conflict with another package, Ubuntu uses the nodejs command for Node.js. To achieve consistency, create an alias from node to nodejs:

sudo ln -s `which nodejs` /usr/bin/node # This is me, your friendly symlink!

Doing the above allows you to run Node.js via the node command, a common expectation for scripts.

Naming conflicts and how to avoid them

The package naming conflict on Ubuntu stems from a non-related node package on other Linux systems. To avoid this collision, Node.js gets the alias nodejs on Ubuntu.

Installing nodejs-legacy

One approach to address this is installing nodejs-legacy, which makes a link from /usr/bin/node to /usr/bin/nodejs:

sudo apt-get install nodejs-legacy # "Legacy" isn't always a bad thing 😉

This simplifies using Node.js as commands generally expect node.

Opting for a different shell

If there continues to be an issue with global command-line tools, try using a modern shell like zsh:

sudo apt install zsh # Welcome to Z-shell, the future of shells chsh -s $(which zsh) # Don't worry bash, you're still in our hearts ❤️

This shell switch makes global tools more compliant, increasing the smoothness of execution.

When node command is still elusive

If node refuses to run despite your efforts, check both /usr/sbin/ and /usr/bin/ for the nodejs binary. As a last resort, consider removing and reinstalling Node.js:

sudo apt-get remove nodejs # Bye for now 👋 sudo apt-get install nodejs # Welcome back! 😄

Using Node Version Manager (nvm)

Why and how to use nvm

If you work on multiple Node.js projects requiring different versions, nvm can be a lifesaver! With nvm, you can switch easily between versions, installing new ones without globally affecting the system.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash # Installing nvm nvm install 14 # Installing Node.js v14 nvm use 14 # Switching to Node.js v14

Perks of using nvm

  • Supports version-specific environments
  • Allows easy upgrades and downgrades of Node.js versions without sudo
  • Providers smoother version management

In case the node command still doesn't work

Alternative commands

If node continues to be problematic, npm and nodejs can replace it:

nodejs script.js # Running a script with nodejs npm start # Starting a script with npm

Repositories and operating system versions

Sometimes, issues could arise from your repositories. Check the Nodesource distributions for any common snags. Remember to always consider compatibility with your OS version. When possible, updating to a newer version of Ubuntu and Node.js could save you a headache or two.