Skip to Content

How to Fix Node and Nodejs Module Version Mismatch on Ubuntu

If you have installed Node.js on Ubuntu using different methods, you may encounter a problem where the node and nodejs commands show different versions. This article will explain why this happens and how to solve it.

What is Node.js and why do you need it?

Node.js is a popular JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. Node.js is often used to create web applications, APIs, and command-line tools. Node.js is also the basis for many frameworks and libraries, such as React, Angular, Express, and npm.

To use Node.js, you need to install it on your system. There are several ways to install Node.js on Ubuntu, such as using the official package manager (apt), using a third-party tool (nvm, snap, etc.), or downloading the binary from the Node.js website. However, these methods may not always be compatible with each other, and may result in different versions of Node.js being installed.

How to check the Node.js version on Ubuntu

To check the Node.js version on Ubuntu, you can use the node or nodejs command followed by the -v or –version flag. For example:

node -v
nodejs -v

How to Fix Node and Nodejs Module Version Mismatch on Ubuntu

These commands should display the same version number, such as v14.17.6. However, in some cases, you may see different versions, such as v10.0.0 for node and v4.2.6 for nodejs. This means that you have two different Node.js packages installed on your system, and they are not in sync.

Why do node and nodejs show different versions on Ubuntu?

The reason why node and nodejs show different versions on Ubuntu is that they are two different packages in the Ubuntu software repository. The nodejs package is the older version that is maintained by the Ubuntu developers, and the node package is the newer version that is maintained by the Node.js developers.

The nodejs package is installed by default when you use the apt command to install Node.js, such as:

sudo apt update
sudo apt install nodejs

The node package is installed when you use the snap command to install Node.js, such as:

sudo snap install node --classic

The snap command is a newer way to install software on Ubuntu, and it allows you to choose between different versions and channels of Node.js. The snap package also includes npm, the Node.js package manager, which is not included in the apt package.

How to fix the node and nodejs version mismatch on Ubuntu

There are several ways to fix the node and nodejs version mismatch on Ubuntu, depending on your preference and needs. Here are some possible solutions:

Fix 1: Uninstall the nodejs package and use only the node package

This is the simplest solution if you want to use the latest and most up-to-date version of Node.js, and you don’t need the apt package for any reason. To uninstall the nodejs package, run:

sudo apt remove nodejs

Fix 2: Uninstall the node package and use only the nodejs package

This is the opposite solution if you want to use the older and more stable version of Node.js, and you don’t need the snap package for any reason. To uninstall the node package, run:

sudo snap remove node

Fix 3: Create a symbolic link from node to nodejs or vice versa

This is a workaround solution if you want to keep both packages installed, but you want to make sure that both commands point to the same version. A symbolic link is a file that points to another file, and it can be used to alias one command to another. To create a symbolic link from node to nodejs, run:

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

To create a symbolic link from nodejs to node, run:

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

Note that you may need to adjust the paths depending on where your Node.js packages are installed. You can use the which command to find out the location of each command, such as:

which node
which nodejs

Fix 4: Use nvm to manage multiple Node.js versions

This is the most flexible solution if you want to have more control over the Node.js versions that you use, and you want to switch between them easily. nvm is a tool that allows you to install and manage multiple Node.js versions on your system, and it creates a separate environment for each version. To use nvm, run:

nvm install <version>
nvm use <version>

Replace <version> with the Node.js version that you want to install and use, such as 14.17.6. You can also use aliases, such as latest, stable, or lts, to refer to the latest, stable, or long-term support versions of Node.js. To see the list of available versions, run:

nvm ls-remote

To see the list of installed versions, run:

nvm ls

Conclusion

In this article, you learned how to fix the node and nodejs version mismatch on Ubuntu. You learned why this problem occurs, how to check the Node.js version on your system, and how to choose and apply a solution that suits your needs.

You also learned how to use nvm to manage multiple Node.js versions on your system. By following these steps, you can ensure that you have a consistent and reliable Node.js environment on your Ubuntu system.