How I installed `nodejs` in Ubuntu 18.04

Enable the NodeSource repository

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

Install nodejs

$ sudo apt install nodejs

Check version
$ node --version
v10.19.0
$ npm --version
6.13.4
Now install npm with npm.

$ sudo npm install npm -g
Funny, right? but it is recommended in a webpage: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm

Important note:
If you run it multiple times You will face a problem.
"ELOOP: too many symbolic links encountered"
to solve this, delete the symlink and install npm again.
$ rm -Rf /home/user/.npm-global/lib/node_modules/npm
$ sudo npm install npm -g
Again check version
$ node --version
v10.19.0
$ npm --version
6.14.4

New vresion of npm installed. right?

Now install a node version manager.

$ sudo npm install -g n
sudo may not be necessary, but it will not make any problem.

I use "n". because, when you change version, it does not delete other version files. So, quickly you can change version.

You have successfully installed n. Run $ sudo n command.
    node/9.6.1
    node/12.10.0
    node/12.14.0
    node/12.16.1

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

But if you see error. See solution bellow.


I found a problem when I run n command:

$ sudo n
sudo: n: command not found

I found that, the problem is related with symlink.

I run $ ls -lah /usr/local/bin/n, and I see red color.
lrwxrwxrwx 1 root root 27 Dec 26 14:25 /usr/local/bin/n -> ../lib/node_modules/n/bin/n
Symlink is not right, because is not our node_modules folder.
$ cd ~/.npm-global/lib/node_modules/n/bin
$ pwd # output "/home/user/.npm-global/lib/node_modules/n/bin"
$ ls # output "n", so there is file "n" inside this directory

Add /n after the output of $ pwd command, and create a symlink.
$ sudo rm /usr/local/bin/n
$ sudo ln -s /home/user/.npm-global/lib/node_modules/n/bin/n /usr/local/bin/n
create another symlink:
$ sudo ln -s /usr/local/bin/n /usr/bin/n

Now you can check symlinks:
$ ls -lah /usr/bin/n
$ ls -lah /usr/local/bin/n
Now both output is green.

And. Now sudo n command works fine.

    node/9.6.1
    node/12.10.0
    node/12.14.0
    node/12.16.1

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

Topic: