How to Install and Use NPM (Node Package Manager) on Ubuntu 17.10
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.
NPM is available on the official package repository of Ubuntu 17.10 Artful Aardvark.
First update your package repository cache with the following command:
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:
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.
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:
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:
Now to search for a Node.js module with NPM, run the following command:
Let’s say you’re looking for a web framework; you would run the following command:
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.
Now to install ‘express’ using NPM, run the following command:
‘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:
‘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:
‘express’ is removed.
To remove ‘express-generator’ global Node.js module, you can run the following command:
‘express-generator’ is removed.
Listing installed Node.js Modules with NPM:
You can list locally installed Node.js Modules with the following command:
You can install globally installed Node.js Modules with the following command:
That’s how you install and use NPM on Ubuntu 17.10 Artful Aardvark. Thanks for reading this article.