/

An introduction to the npm package manager

An introduction to the npm package manager

In this blog post, we will provide a quick guide to npm, the powerful package manager that is key to the success of Node.js. As of January 2017, the npm registry listed over 350,000 packages, making it the largest single language code repository on Earth. With npm, you can be sure that there is a package for (almost!) everything.

Table of Contents

Introduction to npm

npm, short for Node Package Manager, is the standard package manager for Node.js. It initially started as a way to download and manage dependencies of Node.js packages, but it has now become a widely used tool in frontend JavaScript development as well. The npm registry hosts a vast collection of packages that developers can utilize in their projects.

Yarn is an alternative to npm. Make sure you check it out as well.

Installation

To use npm, you must first install Node.js. If you haven’t installed it already, you can download it from the official Node.js website at https://nodejs.org. The npm package manager is automatically installed along with Node.js.

How to use npm

npm is used to manage the dependencies of your project.

Installing all dependencies

If a project has a package.json file, you can use the following command to install all the dependencies required for the project:

1
npm install

This command will install all the dependencies in the node_modules folder, creating the folder if it does not already exist.

Installing a single package

You can also install a specific package by running the following command:

1
npm install <package-name>

You can include additional flags with this command:

  • --save installs the package and adds it to the dependencies section of the package.json file (default behavior as of npm 5).
  • --save-dev installs the package and adds it to the devDependencies section of the package.json file.

The difference between dependencies and devDependencies is that devDependencies are typically development tools, such as testing libraries, whereas dependencies are packages that are bundled with the application in production.

Updating packages

npm makes it easy to update packages. By running the following command:

1
npm update

npm will check all the packages for newer versions that satisfy the versioning constraints specified in the package.json file.

You can also update a specific package by running:

1
npm update <package-name>

Versioning

In addition to managing downloads, npm also handles versioning. This allows you to specify specific versions of packages or require versions higher or lower than what you need.

Versioning is particularly useful when dealing with compatibility issues between libraries or when a bug in the latest release of a library is causing problems. Specifying an explicit version of a library also ensures that the entire team is using the same version until the package.json file is updated.

npm follows the semantic versioning (semver) standard, which allows for clear and predictable versioning of packages.

Running Tasks

The package.json file supports a format for specifying command line tasks that can be run using npm. For example:

1
2
3
4
5
6
{
"scripts": {
"start-dev": "node lib/server-development",
"start": "node lib/server-production"
}
}

This feature is commonly used to run tools like Webpack:

1
2
3
4
5
6
7
{
"scripts": {
"watch": "webpack --watch --progress --colors --config webpack.conf.js",
"dev": "webpack --progress --colors --config webpack.conf.js",
"prod": "NODE_ENV=production webpack -p --config webpack.conf.js"
}
}

Instead of typing long commands, which are prone to mistakes, you can run the following commands:

1
2
3
$ npm run watch
$ npm run dev
$ npm run prod

Tags: npm, package manager, Node.js, JavaScript, frontend development, dependencies, versioning