Introduction

Node.js is a Javascript platform for server-side programming that allows users to build network applications quickly.
JavaScript is a client-side programming language, which means it’s processed within the user’s browser.
With the advent of Node.js, JavaScript can also be used as a server-side language.
By leveraging Javascript on both the front-end and the back-end, development can be more consistent and more feature rich. The name npm is often referred to as an acronym for node package manager though it isn’t official. The npm tool allows users to connect to an online repository containing all the Node.js programs, plugins, modules etc to facilitate easy installation of different programs.

In this article, we will explain how to install and use npm, a Node.js package management system, to manage Node.js packages on your system. We’ll be using a centos 7 system for the purpose of this demonstration.

 

Install npm
Both npm and nodejs are available in the EPEL repository.
So if you have the EPEL repository enabled then you may simply use yum to install the npm package manager tool as we do in the below example.

[root@linuxnix ~]# yum install npm
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 6.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 3.4 kB 00:00:00
puppetlabs-pc1 | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): puppetlabs-pc1/x86_64/primary_db | 135 kB 00:00:02
(2/4): epel/x86_64/updateinfo | 862 kB 00:00:18
(3/4): updates/7/x86_64/primary_db | 4.6 MB 00:00:21
(4/4): epel/x86_64/primary_db | 6.1 MB 00:00:37
Determining fastest mirrors
* base: ftp.iitm.ac.in
* epel: repo.fedoralinux.ir
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package npm.x86_64 1:3.10.10-1.6.12.0.1.el7 will be installed
--> Processing Dependency: nodejs = 1:6.12.0-1.el7 for package: 1:npm-3.10.10-1.6.12.0.1.el7.x86_64
--> Running transaction check
---> Package nodejs.x86_64 1:6.12.0-1.el7 will be installed
--> Processing Dependency: http-parser >= 2.7.0 for package: 1:nodejs-6.12.0-1.el7.x86_64
--> Processing Dependency: libuv >= 1:1.9.1 for package: 1:nodejs-6.12.0-1.el7.x86_64
--> Processing Dependency: libhttp_parser.so.2()(64bit) for package: 1:nodejs-6.12.0-1.el7.x86_64
--> Processing Dependency: libuv.so.1()(64bit) for package: 1:nodejs-6.12.0-1.el7.x86_64
--> Running transaction check
---> Package http-parser.x86_64 0:2.7.1-5.el7_4 will be installed
---> Package libuv.x86_64 1:1.10.2-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Installing:
npm x86_64 1:3.10.10-1.6.12.0.1.el7 epel 2.5 M
Installing for dependencies:
http-parser x86_64 2.7.1-5.el7_4 updates 28 k
libuv x86_64 1:1.10.2-1.el7 epel 109 k
nodejs x86_64 1:6.12.0-1.el7 epel 4.6 M

Transaction Summary
====================================================================================================================================
Install 1 Package (+3 Dependent packages)

Total download size: 7.3 M
Installed size: 26 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): http-parser-2.7.1-5.el7_4.x86_64.rpm | 28 kB 00:00:11
(2/4): libuv-1.10.2-1.el7.x86_64.rpm | 109 kB 00:00:12
(3/4): npm-3.10.10-1.6.12.0.1.el7.x86_64.rpm | 2.5 MB 00:00:36
(4/4): nodejs-6.12.0-1.el7.x86_64.rpm | 4.6 MB 00:01:48
------------------------------------------------------------------------------------------------------------------------------------
Total 69 kB/s | 7.3 MB 00:01:48
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : 1:libuv-1.10.2-1.el7.x86_64 1/4
Installing : http-parser-2.7.1-5.el7_4.x86_64 2/4
Installing : 1:nodejs-6.12.0-1.el7.x86_64 3/4
Installing : 1:npm-3.10.10-1.6.12.0.1.el7.x86_64 4/4
Verifying : 1:npm-3.10.10-1.6.12.0.1.el7.x86_64 1/4
Verifying : 1:nodejs-6.12.0-1.el7.x86_64 2/4
Verifying : http-parser-2.7.1-5.el7_4.x86_64 3/4
Verifying : 1:libuv-1.10.2-1.el7.x86_64 4/4
Installed:
npm.x86_64 1:3.10.10-1.6.12.0.1.el7
Dependency Installed:
http-parser.x86_64 0:2.7.1-5.el7_4 libuv.x86_64 1:1.10.2-1.el7 nodejs.x86_64 1:6.12.0-1.el7
Complete!

 

Check installed version
Now that we have installed npm successfully, let’s verify the version of npm installed with the following command.

[root@linuxnix ~]# npm --version
3.10.10
[root@linuxnix ~]#

 

Getting help
To view the different options available with the npm command, you may use the npm help command as shown below.

[root@linuxnix ~]# npm help
Usage: npm <command>
where <command> is one of:
access, adduser, bin, bugs, c, cache, completion, config,
ddp, dedupe, deprecate, dist-tag, docs, edit, explore, get,
help, help-search, i, init, install, install-test, it, link,
list, ln, login, logout, ls, outdated, owner, pack, ping,
prefix, prune, publish, rb, rebuild, repo, restart, root,
run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, tag, team, test, tst, un, uninstall,
unpublish, unstar, up, update, v, version, view, whoami
npm <cmd> -h quick help on <cmd>
npm -l display full usage info
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
/root/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm@3.10.10 /usr/lib/node_modules/npm

I would like to mention that installing npm also add a man page for the command.

To view a summary of the commands printed in the npm help output, use the npm -l command.
Here is a quick snippet:

[root@linuxnix ~]# npm -l
Usage: npm <command>
where <command> is one of:
access npm access public [<package>]
npm access restricted [<package>]
npm access grant <read-only|read-write> <scope:team> [<package>]
npm access revoke <scope:team> [<package>]
npm access ls-packages [<user>|<scope>|<scope:team>]
npm access ls-collaborators [<package> [<user>]]
npm access edit [<package>]
---------------------------output truncated for brevity

 

Local vs Global package installations using npm

While creating a Node.js application, you create a directory structure to hold that application.
Within the project’s directory, there will be a subdirectory called node_modules.
This holds all of the modules that you can install locally for your project.

Let’s create an empty directory structure that will serve as our package directory as an example:

mkdir -p ~/projects/new_project_package/node_modules
cd ~/projects/new_project_package

Modules installed locally can be used within your project by using the regular Javascript require() method.
This is what you need most of the time for a package.

If you need to use a package’s command line capabilities, you should install it globally.
Globally installed applications are kept in the ~/.npm/ directory.
Their command line functions will be available to all of the Node.js applications.
Globally installed packages cannot be used with the Javascript require() though.

Knowing this, you can see how we will have functionality that is associated with each specific type of package.
By default, most npm commands will assume that you are talking about local packages unless you pass the -g or –global flags.

 

Listing installed packages
To list installed packages in a given directory structure or project, we use the npm ls command.
In the below example, we execute the npm ls command from within the ~/projects/new_project_package directory.

[root@linuxnix new_project_package]# npm ls
/root/projects/new_project_package
└── (empty)

[root@linuxnix new_project_package]#

Since we haven’t installed any Node.js packages for our application yet, this shows up empty.

Npm installs some packages globally automatically during its installation.
Let’s take a look using the npm ls command with the -g option.

[root@linuxnix new_project_package]# npm ls -g
/usr/lib
└─┬ npm@3.10.10
├── abbrev@1.0.9 -> /usr/lib/node_modules/npm/node_modules.bundled/abbrev
├── ansi-regex@2.0.0 -> /usr/lib/node_modules/npm/node_modules.bundled/ansi-regex
├── ansicolors@0.3.2 -> /usr/lib/node_modules/npm/node_modules.bundled/ansicolors
├── ansistyles@0.1.3 -> /usr/lib/node_modules/npm/node_modules.bundled/ansistyles
├── aproba@1.0.4 -> /usr/lib/node_modules/npm/node_modules.bundled/aproba
├── archy@1.0.0 -> /usr/lib/node_modules/npm/node_modules.bundled/archy
├── asap@2.0.5 -> /usr/lib/node_modules/npm/node_modules.bundled/asap
├── chownr@1.0.1 -> /usr/lib/node_modules/npm/node_modules.bundled/chownr
├── cmd-shim@2.0.2 -> /usr/lib/node_modules/npm/node_modules.bundled/cmd-shim
├─┬ columnify@1.5.4 -> /usr/lib/node_modules/npm/node_modules.bundled/columnify
-------------------------------------------------------output truncated for brevity

 

Before we start using npms’ package management capabilities, I would like to talk about an issue I ran into while working with it on my centos 7 system. After installing npm when I searched for a package I received the below error:

[root@linuxnix new_project_package]# npm search react
npm WARN Building the local index for the first time, please be patient
npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference

After some research, I found out that the error was caused because I was using an older version of openssl.
To fix this, I executed the following commands:

[root@linuxnix new_project_package]# yum update openssl
[root@linuxnix new_project_package]# npm rebuild

After this npm was working fine.

 

Search for a package
To search for a package using npm we use the npm search command followed by the package name.
Let’s search for the react package as an example.
React is a JavaScript library for building user interfaces.

[root@linuxnix new_project_package]# npm search react | grep -w '^react'
npm WARN notice update to the newest npm client for improved search results: npmjs.com/get-npm
react React is a JavaScript library for building user interfaces. =clemmy =acdlite… 2017-11-2

The output gives the NAME, DESCRIPTION, AUTHOR and DATE on which the package was added to the repository. The package search will search for the package in the name as well as a description. I received numerous results when I searched for the react package so I filtered the search through the grep utility.

 

Install a package
If we want the package in our local application’s module directory i.e. the directory /root/projects/new_project_package/node_modules we created earlier (so that it can be included in the app with a require() line), we use the following command.

[root@linuxnix new_project_package]# pwd
/root/projects/new_project_package
[root@linuxnix new_project_package]# npm install react
/root/projects/new_project_package
└─┬ react@16.2.0
├─┬ fbjs@0.8.16
│ ├── core-js@1.2.7
│ ├─┬ isomorphic-fetch@2.2.1
│ │ ├─┬ node-fetch@1.7.3
│ │ │ ├─┬ encoding@0.1.12
│ │ │ │ └── iconv-lite@0.4.19
│ │ │ └── is-stream@1.1.0
│ │ └── whatwg-fetch@2.0.3
│ ├─┬ promise@7.3.1
│ │ └── asap@2.0.6
│ ├── setimmediate@1.0.5
│ └── ua-parser-js@0.7.17
├─┬ loose-envify@1.3.1
│ └── js-tokens@3.0.2
├── object-assign@4.1.1
└── prop-types@15.6.0
npm WARN enoent ENOENT: no such file or directory, open '/root/projects/new_project_package/package.json'
npm WARN new_project_package No description
npm WARN new_project_package No repository field.
npm WARN new_project_package No README data
npm WARN new_project_package No license field.

Now if I run the npm ls command again, the newly installed packages will show up in the output.

[root@linuxnix new_project_package]# pwd
/root/projects/new_project_package
[root@linuxnix new_project_package]# npm ls
/root/projects/new_project_package
└─┬ react@16.2.0
├─┬ fbjs@0.8.16
│ ├── core-js@1.2.7
│ ├─┬ isomorphic-fetch@2.2.1
│ │ ├─┬ node-fetch@1.7.3
│ │ │ ├─┬ encoding@0.1.12
│ │ │ │ └── iconv-lite@0.4.19
│ │ │ └── is-stream@1.1.0
│ │ └── whatwg-fetch@2.0.3
│ ├─┬ promise@7.3.1
│ │ └── asap@2.0.6
│ ├── setimmediate@1.0.5
│ └── ua-parser-js@0.7.17
├─┬ loose-envify@1.3.1
│ └── js-tokens@3.0.2
├── object-assign@4.1.1
└── prop-types@15.6.0

In order to download and install the react package and all of its dependencies into the global npm root directory, add the -g option with the npm install command.

npm install -g react

Uninstall a package
To uninstall a package, we use the npm uninstall command followed by the package name.
Let’s demonstrate by uninstalling the react package that we had installed earlier.
Note that since we had performed a local installation, we’ll need to execute the uninstall command from our project directory.

[root@linuxnix new_project_package]# npm uninstall react
- asap@2.0.6 node_modules/asap
- core-js@1.2.7 node_modules/core-js
- iconv-lite@0.4.19 node_modules/iconv-lite
- encoding@0.1.12 node_modules/encoding
- is-stream@1.1.0 node_modules/is-stream
- js-tokens@3.0.2 node_modules/js-tokens
- loose-envify@1.3.1 node_modules/loose-envify
- node-fetch@1.7.3 node_modules/node-fetch
- object-assign@4.1.1 node_modules/object-assign
- promise@7.3.1 node_modules/promise
- setimmediate@1.0.5 node_modules/setimmediate
- ua-parser-js@0.7.17 node_modules/ua-parser-js
- whatwg-fetch@2.0.3 node_modules/whatwg-fetch
- isomorphic-fetch@2.2.1 node_modules/isomorphic-fetch
- fbjs@0.8.16 node_modules/fbjs
- prop-types@15.6.0 node_modules/prop-types
- react@16.2.0 node_modules/react
npm WARN enoent ENOENT: no such file or directory, open '/root/projects/new_project_package/package.json'
npm WARN new_project_package No description
npm WARN new_project_package No repository field.
npm WARN new_project_package No README data
npm WARN new_project_package No license field.

Now if we execute the npm ls command again, the result will be empty.

[root@linuxnix new_project_package]# npm ls
/root/projects/new_project_package
└── (empty)

Conclusion

This concludes our basic introduction to the npm package manager where we learned how to install npm and then use npm to install and remove packages.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.