Install and Use NPM in Ubuntu 19.10

NPM is the default package manager for Node.js. It has been written in JavaScript. In previous article, we discussed how to install Node.js in Ubuntu 19.10. We can consider this article as an extension to it. In this article, we would discuss how to install and use NPM in Ubuntu 19.10 release.

NPM is already included in Node.js package, which can be downloaded from the official website. Alternately, we could install NPM from the standard Ubuntu repository also.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

Method: I – Install NPM in Ubuntu 19.10 through repository

We need to first update the standard Ubuntu repository to make the latest version of the package available.

sudo apt update

Thereafter, issue the following –

sudo apt install npm

Once the package and related dependencies are installed, we can verify through –

npm -v

It would return with the version of NPM installed. In our case, it was version 5.8.0.

It is worth mentioning here that, NPM package installed through standard Ubuntu repository may be different from the latest LTS version available on official website.

Alternately, we could install NPM through the package available on official website itself.

Method: II – Install NPM (Linux Binaries) in Ubuntu 19.10

As already discussed, the NPM package is already included in the Node.js package – which can be downloaded through Nodejs official website. Head to the Downloads section on Nodejs official website to download the file – Linux Binaries (x64)node-v12.13.0-linux-x64.tar.xz

Its of around 13.4 MBs in size. The downloaded file includes NPM version 6.12.0.

Next, extract the file through tar command-line utility.

tar -xvf node-v12.13.0-linux-x64.tar.xz

It would create a directory node-v12.13.0-linux-x64/ in the current folder.

Now, open ~/.bashrc file using a text editor like nano –

nano ~/.bashrc

and, append the file with the following –

export PATH=/path/to/node-v12.13.0-linux-x64/bin:$PATH

where, /path/to/ should be replaced.

Lastly, issue the following in terminal to verify –

npm -v

It would return with the package version installed i.e. 6.12.0.

Uptill now, things are pretty similar to what we did while installing Node.js. To make NPM accessible to root/superuser, we need to create a symbolic link. Therefore, issue the following in terminal –

sudo ln -s /path/to/node-v12.13.0-linux-x64/bin/npm /usr/bin/npm

Login as root or sudo -i to verify –

npm -v

Install & Use NPM Modules

Before, installing any modules through command-line utility npm – we need to first initialize it for our project. So, navigate to your project folder and issue the following in terminal –

npm init

It will ask you a few questions. And, thereafter a new file package.json will be created.

If you don’t initialize, then you may get the following warning –

npm WARN saveError ENOENT: no such file or directory, open '/home/<user-name>/package.json'

Now, to install a NPM package/module –

npm install <module_name> --save

for instance to install lodash module,

npm install lodash --save

where,

–save, updates the contents of file package.json.

Now, we can append our project file with the following;

const _ = require("lodash");

where,

_ is specifically for lodash module.

In conclusion, we have discussed how to install and use NPM in Ubuntu 19.10 release. More about npm modules in subsequent articles.

Similar Posts