Ubuntu

Install NPM NodeJS Package Manager on Ubuntu

How to Install and Use NPM (Node Package Manager) on Ubuntu 17.10

JavaScript is one of the most popular programming language in the world. No wonder that it’s so popular. It is the only programming language that web browsers understand. Every website in the world uses JavaScript to deliver interactive content to the visitors. Without JavaScript you would have a plain website and the page would reload every time you clicked. The web would not be what it is now.

But there is a problem with JavaScript. It runs on a Web Browser only. So on the server side you had to use something like PHP, Java, Python, Ruby and other programming languages. For a long time, developers wanted to use their favorite JavaScript on the server instead of PHP, Java and other programming languages. Node.js is one of the solutions for JavaScript on the server.

Node.js is very modular in nature. The language provides only the basic functionalities and it’s easily extended through the use of Node modules. There are a lot of Node modules that you can choose from. If you’re a developer, you should find some module or many that does what you want. But where do you find these modules? Well, there’s a repository where all the Node.js modules are kept. You can go to https://www.npmjs.com to search for Node.js modules.

Since Node.js is a modular language, and can be extended with Node.js modules, each of these modules depend on other Node.js modules. Installing Node.js modules manually is a tedious task and very impractical. This is where a Node Package Manager or NPM in short, comes in. With NPM, you can easily install, remove, update any module you want.

In this article, I am going to show you how to install and use NPM on Ubuntu 17.10 Artful Aardvark. Let’s get started.

Installing NPM:

NPM is available on the official package repository of Ubuntu 17.10 Artful Aardvark.

First update your package repository cache with the following command:

$ sudo apt-get update

Since NPM is a Node.js package manager, there’s no point in installing only NPM. So you can install Node.js and NPM together with the following command:

$ sudo apt-get install nodejs npm

Press ‘y’ and then <Enter> to continue.

Node.js and NPM should be installed.

Now run the following command to verify whether Node.js is working correctly.

$ node --version

You should see similar output as shown in the screenshot. It means Node.js is working correctly.

Now do the same for NPM with the following command:

$ npm --version

So NPM is working correctly as well.

Searching for Node.js Modules:

You can search for a Node.js module using NPM without going to https://www.npmjs.com

First update the NPM package repository cache with the following command:

$ npm update

Now to search for a Node.js module with NPM, run the following command:

$ npm search YOUR_QUERY

Let’s say you’re looking for a web framework; you would run the following command:

$ npm search “web framework”

You can see that search result is shown.

Installing Node.js Module using NPM:

You can install a Node.js module globally or locally. The difference is that, global node modules can be accessed like any other commands, but it’s not the case for locally installed modules. Soon you will see the difference.

I am going to install “express” module locally to show you how to install Node.js module with NPM.

Before I go any further, I am going to create a directory for my Node.js project and navigate to the directory.

$ mkdir node-demo && cd node-demo

Now to install ‘express’ using NPM, run the following command:

$ npm install express

‘express’ module should be installed and it should be stored in the ‘node_modules’ directory as shown in the screenshot below. When you install it locally, this subdirectory is created.

You can also install a module globally. Let’s say you want to install ‘express-generator’ globally, you would be running the following command:

$ sudo npm -g install express-generator

‘express-generator’ is installed.

Now ‘express-generator’ can be accessed from the command line.

Removing Node.js module with NPM:

You can remove a local Node.js module with NPM with the following command:

$ npm remove express

‘express’ is removed.

To remove ‘express-generator’ global Node.js module, you can run the following command:

$ sudo npm -g remove express-generator

‘express-generator’ is removed.

Listing installed Node.js Modules with NPM:

You can list locally installed Node.js Modules with the following command:

$ npm list

You can install globally installed Node.js Modules with the following command:

$ npm -g list

That’s how you install and use NPM on Ubuntu 17.10 Artful Aardvark. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.