Jun 09, 2020 5:10 PM Jun 04, 2021 3:00 AM

Installing npm on Debian/Ubuntu

In this article I will explain how to install npm on the Debian/Ubuntu operating system. npm is a package manager for the JavaScript programming language. This is one of the Node.js tools. Therefore, we need to install Node.js to access npm. In this instruction we will use NodeSource. Newer versions of Node.js will be available to you than in the official Debian/Ubuntu repositories. Node.js 16.3.0 (includes npm 7.15.1) is the latest stable version available for installation.

This instruction was executed on operating systems: Debian 10, Debian 9, Ubuntu 20.10, Ubuntu 20.04, Ubuntu 19.10.

Installing Node.js and npm

Install the curl utility by running the command in the terminal:

sudo apt-get install curl

Download the installation script using curl, and then run it:

You can choose another available version on the next page https://nodejs.org/en/download/releases/.

Install Node.js and npm by running the command in the terminal:

sudo apt-get install nodejs

Installing Node.js and npm

Testing Node.js and npm

Let's find out the version of installed Node.js by running the command in the terminal:

node -v

And now we find out the version of npm installed by running the command in the terminal:

npm -v

Now let's see how to use Node.js and npm in a project. Create the root directory of the project and go to it.

mkdir ~/www-project && cd ~/www-project

Then all the commands are executed at the root of your project.

Initialize the project to create package.json by running the command in the terminal:

npm init

We will use the jsdom package as an example. Install this package by running the command in the terminal:

npm install jsdom

Two package.json and package-lock.json files, as well as the /node_modules directory, will be created in the project directory. Run the command in the terminal to see this:

ls -l

Testing Node.js and npm

Let's create a index.js file and add the following code to it:

nano index.js
const jsdom = require('jsdom');
const { JSDOM } = jsdom;

const dom = new JSDOM('<!DOCTYPE html><p>Hello world</p>');
console.log(dom.window.document.querySelector('p').textContent); // Hello world

This script will output the text "Hello world" by running it in the terminal:

node index.js

Package management

You may need the following commands to manage packages.

Installing packages

To install defined packages of your project, run the command in the terminal:

npm install

To install only one package, specify the package name at the end:

npm install jsdom

Updating packages

To update packages, run the command in the terminal:

npm update

To update only one package, specify the package name at the end:

npm update jsdom

Deleting package

To remove a specific package, run the command in the terminal:

npm uninstall jsdom

Other articles