NodeJS

Install NPM on Debian 9

How to Install and Use NPM on Debian 9 Stretch

NPM or Node Package Manager is the same thing as APT to Debian. It is used to install, remove, update NodeJS packages.  In this article, I will show you how to install and use NPM on Debian 9 Stretch.

Installing NodeJS and NPM

The version of NodeJS available in the official Debian 9 Stretch package repository is 4.x, which is very old. The official package repository of Debian 9 Stretch does not have NPM.  In this section, I will install the latest stable version of NodeJS and NPM which is at the time of this writing 8.11.1.

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

$ sudo apt-get update

Now install CURL with the following command:

$ sudo apt-get install curl

Press y and then press <Enter> to continue.

CURL should be installed.

Now add the package repository of NodeJS 8.x with the following command:

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

The NodeJS 8.x package repository should be added as you can see from the screenshot below.

Now you can install NodeJS and NPM all together along with the development tools for building native NodeJS modules with the following command:

$ sudo apt-get install build-essential nodejs

Now press y and then press <Enter> to continue.

NodeJS, NPM and the required build tools for compiling NodeJS native modules are installed.

Now check whether NodeJS is working with the following command:

$ node --version

As you can see, NodeJS 8.11.1 is installed and working correctly.

Now check whether NPM is working with the following command:

$ npm --version

As you can see NPM is installed correctly as well.

Using NPM

In this section, I will show you how to use NPM to manager NodeJS modules or packages. Before that, let’s talk about global packages and local packages.

If you install a NodeJS package globally, then it should be saved somewhere /usr/lib/node_modules directory and you can access it from any NodeJS project.

If you install a NodeJS package locally, then a directory node_modules/ should be created on your project directory and the package should be saved in node_modules/ directory relative to your project directory. It can only be accessed from that project.

That’s all you need to know for now. Let’s continue.

Creating a package.json Configuration File with NPM:

Every NodeJS project has a package.json file in its project’s root directory. This file holds information about your project, such as the name of the project, the version of the project, the dependencies or other NodeJS packages your project depends on and many more.  You can manually create a package.json file and put all these information or you can use NPM to create it for you.

First create a new directory node-project (you can name is whatever you want) for your NodeJS project with the following command:

$ mkdir node-project

Now navigate to your project directory with the following command:

$ cd node-project

Now to create a package.json file with NPM, run the following command:

$ npm init

Fill in the details and press <Enter> to move forward.

In the end, type in yes and press <Enter>.

As you can see, a package.json file is created.

This is the contents of the package.json file:

Installing a NodeJS Package Locally with NPM:

If you know the name of the package, then you can easily install it using NPM.

For example, if you want to install express NodeJS package, which is Express web framework, run the following command from your project directory:

$ npm install express --save

express NodeJS package should be installed.

Installing a NodeJS Package Globally with NPM:

You can install a NodeJS package globally from any directory.  For example, if you want to install express NodeJS package globally, run the following command:

$ sudo npm -g install express

It should be installed globally.

Removing a NodeJS Package Locally with NPM:

If you want to remove a NodeJS package, let’s say express, from your project directory, run the following command from your project directory:

$ npm uninstall express --save

The express NodeJS package is removed.

Removing a NodeJS Package Globally with NPM:

To remove a NodeJS package, let’s say express, globally, run the following command:

$ sudo npm -g uninstall express

It should be removed.

Searching for NodeJS Packages:

Well, now you know how to install and remove NodeJS packages with NPM. Now the question is, how do I know what I can install with NPM? What packages are available?

Well, you can search for packages in the NPM’s official website at https://www.npmjs.com/ or you can use NPM command line utility.

To search for NodeJS packages from your web browser, go to https://www.npmjs.com/ and search for what you’re looking for. Type in the keywords as marked in the screenshot below, and press <Enter>.

You should see a lot of packages as you can see in the marked section of the screenshot below. You can click on any one of them to see more information about that package.

You can also run the following command to search for NPM packages:

$ npm search "Web framework"

As you can see, the same list is displayed. You can pick up the package name from the first column as marked in the screenshot below, and install what you need using NPM.

So that’s how you install and use NPM on Debian 9 Stretch. Thanks for reading this article.

About the author

Shahriar Shovon

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.