0

I have updated Node js by following these steps:

sudo npm install -g n
sudo n stable
sudo n latest

However when I check the version with:

node -v

or

nodejs -v

I get the old/current version which is v10.19.0

any idea why this is happening?

Sevicode
  • 55
  • 6

4 Answers4

2

The Node.js v10.19.0 Conundrum


"I am going to answer this question in reference to Ubuntu, because I know about the apt package maintainer, and I feel that even if you don't use Ubuntu as your distro, so long as your a Linux user, you have still used apt/apt-get."

This question demonstrates the method used by NVM for installing Node on a platform like Linux.

NVM makes it extremely easy to install node. Not to long ago everyone had to use curl/wget & npm...

Honestly I can't remember all the steps, but it seemed very involved for an open source language that is extreamly popular, and heavily used on all platforms, henceforth; NVM came about, and grew fast in popularity.


The Problem with NVM


The problem with NVM is the same thing that makes it so damn useful — "to be clear I do really love the Node Version Manager" — NVM uses environment variables, and local installation folders to isntall node, and to configure the environment to use the NVM and NODE commands. The problem is you already have the apt package-maintainer's version. _Apt software is always behind the most current released version, and this is true for almost all software that we could discuss in relation to apt. Your Debian Linux "Out-of-the-Box" install, comes with the latest apt version of node, which is (at the time of authoring this answer):

Node.js v(10.19.00)

...and It gets installed, and assigned to the path variables in the system root binary directory @...

/usr/bin



If you are experiencing this issue, you should do a couple things to gather some info. First, go to your console, and type in the command which node.

Here's a snippet that shows the commands you want to type that'll help you troubleshoot the issue:
# First type the command like this:

~$> which node


# Then do it like this:

~$> which -a node

Your Output should look somewhat like this:

enter image description here

!IMPORTANT!: Also use the command node --version or node -v so you know what your shell thinks your current version is. Its important to know what your shell is doing.

The first which command shows what node installation is being used when you use the command ~$> node, the second which -a command, shows you all of the executable node programs you have throughout your system. You will see multiple, and this is why when you try using npm or other software with nod version is used when you use the command

the next thing you want to do is type the following command

~$> nvm which current

nvm which current (NVM Command for view Node version)


Most every thing should be pointing to a node install located on your path, with a pathname that looks close to something like this:

/home/<your-user-name>/.nvm/versions/node/<current-node-version>

The problem is if you try to do something, like install vsce the extension packager for VSCode with npm, it wont install, even though node seems to be working fine.

So what you have to do is remove the other version of node you have, that you probably seen while executing the commands above. Currently /usr/bin/ has nodejs which is node, and it is the version of node we talked about which is: `node v10.19.00.



Removing the unwanted Node Package & Fixing NPN:

The last thing we will do before removing the unwanted nodejs package is doctor up npm. Use the two commands below, but just read what the output says. It will likely show unpleasant errors, and may not work at all, but just type them in, so we can use them in a bit to fix npm w/ the new node.js version, and you can see the difference. This will help demonstrate, under the hood, everything your doing, and how it is affecting the software on your machine.


okay go ahead and execute away:

~$> npm doctor

then use

~$> sudo npm doctor


Now We Will Remove the Un-wanted package: "Node v10.19.00"

First, lets look at your packages.

To do that use the command below to view your packages. Look and confirm that nodejs is in the list

~$> dpkg --list

then use..

sudo apt-get --purge remove nodejs; sudo apt-get autoremove;

# OR alternatively you could do it in two commands:

sudo apt --purge remove nodejs
sudo apt --purge autoremove

Now use...

~$> dpkg --list
...to confirm that nodejs is indeed gone.



With the unwanted node package out of the way, lets run the following again:

You will likely get unhappy results again, but thats okay, you should notice a bit of a change in behavior from the npm doctor though.
~$> npm doctor

then use

~$> sudo npm doctor

---
The output probably looks like this:

enter image description here



Now you need to copy your NVM version of Node.js (currently I am using Node v17.1.0 but by the time you read this, you will likley be using another version.


To copy your _NVM Node.js Executable to the correct directory, you must do the following:
  1. Use the nvm which current command again, and if you remember, it will print the name of a Node executable that looks like this:

/home/<your-user-name>/.nvm/versions/node/<current-node-version>

  1. Copy & Paste the <NODE-PATHNAME> the Node executable that was logged into your console.

  2. Execute the following command:

~$> sudo copy -v <NODE-PATHNAME> /usr/bin


Now if you don't have the following directories create them:


~$> mkdir ~/node_modules
~$> mkdir ~/node_modules/.bin

!NOTE:

Now upgrade npm. Unlike Node, we won't use an installed package from NPM, and copy it to the places where we want it. We will just makesure both locations have the same npm version
Just FYI, NPM is having the same issue as node.js, but you may have figured that out already.

Use the following commands:

The first npm install will be executed as seen in the snippet

~$> nvm install-latest-npm



The second npm install (the one just below this text) needs a little adjustment, after npm@ you have to replace the word latest with the most recent version of npm.

~$> npm install -g npm@latest

Now lastly you need to use the npm doctor to make sure every thing is working:

~$> npm doctor
~$> sudo npm doctor

You should get a result that looks like this:

enter image description here

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
0

So I figured out how to to upgrade to the latest version of node js. First you need to remove the older version of node with this syntax:

sudo apt-get purge --auto-remove nodejs

Install npm with:

sudo apt install npm

For me it already installed the latest version of node js, however if it did not work for you continue by installing node with these syntaxes:

npm install n -g
n stable
sudo n latest

Then check your node version by running node -v

Sevicode
  • 55
  • 6
  • You probably want to read the answer I authored. You sorta fixed part of the problem, but now you don't have any version of Node on your root path. Test out your install, and your NPM, by using the `npm doctor` command. – JΛYDΞV Nov 23 '21 at 20:58
0

I also updated node js to 16.5.1 version but still showing 10.19.0 version. A simple command worked for me:

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

Here, 12.x with version.For 16th version installation:

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

Then, run following command:

sudo apt-get install -y nodejs

Now check version with node -v

Hope it help.

Brijesh Ray
  • 779
  • 8
  • 15
-1

Consider using nvm and specifying the version you would like to use:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
nvm install 13.10.1

You can try clearing you cache if you want to edit your global node version: sudo npm cache clean -f

tbrk
  • 173
  • 1
  • 8
  • Thanks but I have not installed it with nvm. Also I just found out that first I need to remove the older version. – Sevicode Nov 22 '21 at 03:45
  • Yes you do need to remove the older. If you read the answer I posted youll see that theres actually quite a bit invloved in getting node properly configured on your machine – JΛYDΞV Nov 23 '21 at 21:00