Home > Net >  Node versions do not match: node vs. sudo node -v ... WSL2 Ubuntu 22.04.1
Node versions do not match: node vs. sudo node -v ... WSL2 Ubuntu 22.04.1

Time:01-01

As you can see in the image below, I see two different versions of node depending on which command I run.

CLI

I need the newer version, but npm sees the old version.

https://cdn.discordapp.com/attachments/485329207518298123/1057604620937080862/image.png

Many times I've removed, purged, reinstalled, etc.

I tried installing nvm as root and setting the node version there, but that didn't help either.

If I use apt install nodejs instead of nvm, it tells me that I already have the newest version, which it believes is 12.22.9.

How do I get npm to recognize the newer version (18.12.1) of node that I installed via nvm?

CodePudding user response:

It is because you have a different version of the Node.js which is installed for the root and that particular user.

First, you need to uninstall Node.js:

sudo apt-get remove nodejs

or

sudo npm rm npm -g

If you have any problem with the above commands, then after running which node command, go to that directory, and run the following commands:

rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1

Do the same thing for the current user if needed.

The default Ubuntu/Debian package manager does not have the latest Node.js, and that's why whenever you try to install Node.js with apt install nodejs it says you have the latest version.

According to the official Node.js documentation, for installing the latest version, you should follow these steps:

Using Ubuntu

curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

P.S: No need to run npm or node with sudo. Therefore, I highly recommend you to not use every command with sudo.

  • Related