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:

npmCode language: JavaScript (javascript)

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

npm -vCode language: JavaScript (javascript)

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 initCode language: JavaScript (javascript)

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 --yesCode language: JavaScript (javascript)

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>Code language: JavaScript (javascript)

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 expressCode language: JavaScript (javascript)

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"
}Code language: JavaScript (javascript)

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>Code language: JavaScript (javascript)

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-devCode language: JavaScript (javascript)

For instance:

npm install morgan --save-dev Code language: JavaScript (javascript)

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"
}Code language: JavaScript (javascript)

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 installCode language: JavaScript (javascript)

Install a package globally on your system

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

npm install <package_name> --globalCode language: JavaScript (javascript)

Or in short:

npm i <package_name> -g
Code language: JavaScript (javascript)

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 tutorial helpful ?