You can install Node.js and npm directly from NodeSource repository. Node.JS provides an easy to use a bash script for this purpose.
What you have to keep in mind that you need to specify which major version of Node.Js you want to install.
Let’s say you want to install Node.Js version 11. You can use the following commands:
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
To install the Node.js package, use the following command. Npm is also installed with this package.
sudo apt install nodejs
To upgrade Node.js, please execute the following:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
And in the end, check the installation.
node --version
npm --version