What is npm

Summary: in this tutorial, you will learn how about npm and how to use the npm CLI to install new packages.

Introduction to npm #

Npm stands for Node Package Manager. It is a package manager for the Node JavaScript platform.

Npm is known as the world’s largest software registry. Open-source developers all over the world use npm to publish and share their source code.

Npm consists of three components:

  • The website allows you to find third-party packages, set up profiles, and manage your packages.
  • The command-line interface or npm CLI that runs from a terminal to allow you to interact with npm.
  • The registry is a large public database of JavaScript code.

To find the npm CLI on your computer, you run the npm command from a terminal:

npm

For example, the following command will display the current npm version on your system:

npm -v

What can you do with npm? #

npm allows you to install a new package from the registry. This is what you will do most of the time with npm.

On top of this, npm allows you to discover and publish your new node packages.

package.json #

In general, every npm project has a file called package.json located in the root directory. The package.json is a plain text file that contains important information that npm uses to identify the project and handle dependencies.

To create the package.json file, you go to the root directory of the project and execute the following command:

npm init

When you run the npm init command, it will prompt you for the project information including:

  • Package name
  • Version
  • Test command
  • Git repository
  • Keywords
  • Author
  • License

If you hit Return or Enter, it will accept the default values and move on to the next prompt.

If you want to use default options, you use the following command:

npm init --yes

Later, you can change the default values in the package.json.

Install a new package #

To install a new package, you use the following npm install command:

npm install <package_name>

In this command, you place the package name after the npm install keywords.

To find packages, you go to the npm website and search for them.

For example, if you want to install the express package, you can run the following command:

npm install express

Note that express is a fast web framework for Node.

Once the installation is completed, you will see a new directory called /node_modules created under the root of the project. All the new modules that you install will be placed in this directory.

If you expand the /node_modules directory, you will see that npm installed not only express but also the dependencies of express, and dependencies of these dependencies, and so on.

If you open the package.json file in the root of the project, you will also find that the dependencies section is updated, which includes the express package like this:

"dependencies": {
   "express": "^4.17.1"
}

In general, any new package that you install will be listed in the dependencies section. In this example, the dependencies include the express package with version 4.17.1. Notice that Npm follows the semantic versioning specification.

To save some typing, you can use a shorter version of the npm install command:

npm i <package_name>

In this command, i stands for install.

Install a package as a development dependency #

Sometimes, you may want to install a package that runs only on the development environment.

For example, you may want to install a package that logs HTTP requests such as the morgan package.

To do this, you the npm install command with the --save-dev option with the following syntax:

npm install <package_name> --save-dev

For instance:

npm install morgan --save-dev

This command will download and install the morgan package. In addition, it adds a new section to the package.json file called devDependencies like this:

"devDependencies": {
   "morgan": "^1.10.0"
}

Basically, the devDependencies should contain packages that you use during development. These packages are necessary only while you are developing your application.

On the other hand, the dependencies should contain packages on which your application will depend. In other words, without these dependencies packages, your application will not work.

Also, you can execute the npm install command to download and install all packages listed on the dependencies and devDependencies section:

npm install

Install a package globally on your system #

To install a package globally on your system, you use the following command:

npm install <package_name> --global

Or in short:

npm i <package_name> -g

Generally, you install a package globally when you want to use it in your command line or shell.

If you want a package that you will include in your application, you should install it locally.

Summary #

  • Npm is the package manager for the Node JavaScript platform.
  • Use npm install <package_name> to install a new package.
  • Use npm install <package_name> --save-dev to install a new package as a development dependency.
  • Use npm install <package_name> -g to install a package globally.

Was this helpful?