How to install npm packages?
Tags: node, npm, javascript
Published: 2021-06-08 19:39:19

---
title: How to install npm packages?
published: true
date: 2021-03-14 00:00:00 UTC
tags: node, npm, javascript
canonical_url: https://www.mariokandut.com/how-to-install-npm-packages/
---

Installing dependencies is a core part of working with any Node.js project.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#installing-dependencies)Installing dependencies

Before an application can run, you need to install all existing dependencies from the `package.json` file. You can also add new packages as dependencies or devDependencies.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#installing-dependencies-from-packagejson)Installing dependencies from package.json

The most common way to interact with the npm CLI is through installing packages with `npm install <package>` command from [npmjs.com](https://www.npmjs.com). When you check out a new project repository and try to start the application with `npm start`, you will likely get this error - `Error: Cannot find module`. This means, you forgot to install the project's dependencies.

To install a project's dependencies, navigate in the root folder of the application, and run:

```
npm install
```

You'll see a progress bar as npm begins downloading the dependencies, and a `node_modules/` folder will be created in the project's root directory. A `package-lock.json` file will also be created, if none existed in the project already. Check out the article - [What is package-lock.json](https://www.mariokandut.com/what-is-package-lock-json/), if you are not familiar with this file.

Basically running `npm install` (with no arguments) reads the list of dependencies from the `package.json` with the applicable package versions and version locks(`package-lock.json`), downloads the dependencies, and puts them into the `node_modules/` folder. Once the dependencies are added into the `node_modules` folder, they are found by the application.

Most npm commands have an alias, so you don't have to type the entire command. Just type `npm i` and `npm install` will be executed.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#installing-new-dependencies)Installing new dependencies

The command `npm install` can also be used to add new dependencies. You can find packages on [npmjs.com](https://www.npmjs.com). Just run `npm install <package-name>` to add the new dependency. For example, to install the popular Express server framework as a dependency:

```
npm install express
```

After running the command `npm install express`, you will see a progress bar as npm fetches the package. The package will be downloaded from NPM, installed to your `node_modules/` folder, and added to the dependencies in `package.json`. The `package-lock.json` file also updated, which will happen whenever you add or update dependencies. If `express` is added to a fresh package.json, after `npm init`, the `package.json` might look like this:

```
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}
```

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#install-new-devdependency)Install new devDependency

When installing a package we can choose to record it in the `package.json` as an entry to **dependencies** or **devDependencies**. You can learn more about the difference between `dependencies` and `devDependencies` in the article - [what is package.json](https://www.mariokandut.com/what-is-package-json-and-how-to-configure/). To sum up, `dependencies` are used during production, and `devDependencies` are used only in development or during a build step.

By default, `npm install` adds the package as an entry to `dependencies`, when the flag `--save-dev`, or it's alias `-D`, is added, the package will be installed as a `devDependency`.`ESLint` would be a typical devDependency.

```
npm install --save-dev eslint
```

After running this command, an entry should be added to `devDependencies` in the package.json:

```
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "eslint": "^7.28.0"
  }
}
```

You can also **install packages globally**. This will be covered in a separate article, since there can be errors, like `EACCES`, and `npx` is a great addition to run an arbitrary command from an NPM package. Maybe you don't need to install a package globally, since it can only be used with one installed Node.js version (another Node.js version, would require a new global install).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Installing dependencies is a core part of working with any Node.js project.
- Install the project dependencies listed in the `package.json` with `npm install`.
- Some NPM cli commands have an alias (`npm i` for `npm install`).
- Install a new dependency with `npm install <package>`.
- Install a new devDependency by passing the flag `--save-dev`, like `npm install <package> --save-dev`.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[HeyNode](https://heynode.com/tutorial/create-packagejson-file), [NPM Documentation](https://docs.npmjs.com/cli/install)