Search anything:

npm (Node Package Manager)

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 20 minutes | Coding time: 5 minutes

In this article we will discuss about npm which is one of the most famous package manager in current software world. npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Nodejs.

npm basically has two two products, one is the CLI (Command Line Interface) Application and the other is the online database of public and private packages, called the npm registry (database of JavaScript packages). Infact npm registry is the largest software registry in the world. Node Package Manager is used to install node packages or libraries which can be used in developing projects, publish libraries to the online database and to discover new and powerful libraries.

Here is a link to explore npm packages/libraries - npmjs.

npm CLI

npm CLI allows a developer to install npm packages from npm registry and publish on the online database of packages. There are various npm commands that we will encounter and use quite frequently but before diving deep into these commands we will first undestand about package.json files.

Understanding package.json file

package.json file is simply a file in json format which includes metadata about project like its name, description, author, scripts, version alongwith the dependencies, devDependencies and packages the project depends upon.

The basic structure of package.json file looks something like this:

"name": "",         //name of the project
"version": "",      //current version of project 
"description": "",  //description of the project
"author": "",       //author's name
"license" : "",     //license provider for the project
    "dependency1": "^1.0.0",   //depedencies with their versions installed
    "dependency2": "^1.0.0",

It is not necessary to genearte the package.json file manually instead there is a npm command which will automatically generate this file for your project. We will discuss it in the later section.

Difference between dependencies and devDependencies

In simple terms dependencies are those modules which are needed for production and devDependencies are those modules which are needed only at the time of development. For example we need express both at the time of development and production so it is to be saved in dependencies while nodemon(a tool that restarts the server everytime a change is made) is needed only at the time of development.

npm Commands

1. npm init

Syntax :

npm init   //initialize a node project

npm init command is used to initialize a node project. It will ask input for few project aspects like project's name, initial version, description, author, license, project's git repository, etc. We can choose to fill all details or we can skip some by pressing enter key on each prompt. After this a package.json file will be generated and placed in the current working directory.

2. npm install

It is also one of the most useful command while working with node projects. It is used to install modules or packages from the online npm registry.

Syntax :

npm install <module_name>  //alternatively we can use i as an alias for install 

Example Code :

npm i chalk    //installs chalk module.

Instead of providing a name for module if we simply write npm install then it will trigger the download of all the dependencies and dev-dependencies that are listed in package.json file.

Saving a module as a devDependency

To save a module as a dev-dependency we simply have to provide a flag --save-dev at end of npm install command. Below is an example to illustrate it.

npm install nodemon --save-dev

Installing a module globally

Some modules like nodemon, chalk will be required in many projects so it will be better to install them globally in order to make them available for every project.

Example Code :

npm install nodemon -g    // alternatively we can also use --global flag.

Uninstalling npm packages

In order to remove a package from project we use npm uninstall command. npm unistall takes following exclusive, optional flags which save or update the package version in package.json file:

a). --save removes package from dependencies.
b). --save-dev removes package from devDependencies.
c). --no-save package will not be removed from package.json file.
d). --save-optional package will not be removed from optionalDependencies.

Below are some examples to illustrate it in detail.

Example Code :

npm uninstall chalk    //removes chalk package from project...

npm uninstall chalk --save   //removes chalk from project and also from dependecncy list in package.json file.

npm uninstall chalk --global   //removes chalk from global context...

To learn more about npm commands in detail check out the official npm documentation via this link npm documentation.

So through this article we have learned about what is npm and how we can use it to create a node project and to easily install packages from world's largest library of software packages, npm registry. Now we can develop our own node projects and manage them easily.

How can we check the current version of npm installed ?

npm --version
npm -v
Both npm --version and npm -v
None of the above
Both flags --version and -v will give the currently installed version of npm on the system.

With this article at OpenGenus, you must have a complete idea of NPM (Node Package Manager). Enjoy.

npm (Node Package Manager)
Share this