Wednesday, May 15, 2019

Install NodeJS onto Ubuntu

The task sounds quite simple, but after I failed to do it several times I discovered that would be good to make a post about it. Just to have a step-by-step guide to save time when I decide to do it next time.

Firstly, let's remove old Node leftovers from previous installations
~$ sudo apt remove node
~$ sudo apt remove nodejs

and remove the node folder as well
~$ rm -rf node_modules/

That's time to download and install it in appropriate way.  Download it from Nodesource   https://github.com/nodesource/distributions  - there are info about versions and instructions how to install on the github page.
So we go the the /tmp folder and download the latest NodeJS package
~$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
then install it ~$ sudo apt install -y nodejs

Now we have to install the latest version of NPM
~$ sudo npm i -g npm@latest

Let's check if everything is installed as expected
~$ nodejs -v         v12.2.0
~$ npm -v             6.9.0

Now let's change a directory for global installations. It is necessary to avoid EACCES errors.
~$ sudo mkdir ~/.npm-global
~$ npm config set prefix '~/.npm-global'

Update our PATH variable
~$ echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
~$ source ~/.bashrc

To check that it has been changed use the command
~$ npm config get prefix
We must see something like
/home/username/.npm-global

We can check all installed global packages with the following command
~$ npm list -g
It is empty for now
/home/username/.npm-global/lib
└── (empty)


If we install anything, let's say react, we will be able to see it on the package list
/home/username/.npm-global/lib
└─┬ react@16.8.6
     ├─┬ loose-envify@1.4.0
     │   └── js-tokens@4.0.0
     ├── object-assign@4.1.1
     ├─┬ prop-types@15.7.2
     │   ├── loose-envify@1.4.0
     │   ├── object-assign@4.1.1
     │   └── react-is@16.8.6
     └─┬ scheduler@0.13.6
          ├── loose-envify@1.4.0
          └── object-assign@4.1.1

No comments:

Post a Comment