How to install npm packages?

How to install npm packages?

Generate a package.json file - manually or automatically

By Mario Kandut

Europe’s developer-focused job platform

Let companies apply to you

Developer-focused, salary and tech stack upfront.

Just one profile, no job applications!

This article is based on Node v16.14.0.

Installing dependencies is a core part of working with any Node.js project. If you are just starting with Node.js, have a look at this article - how to uninstall npm packages.

Installing dependencies

💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.

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.

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 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, 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.

Installing new dependencies

The command npm install can also be used to add new dependencies. You can find packages on 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"

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. 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).


  • 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.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

HeyNode, NPM Documentation

More node articles:

How to create a web server in Node.js

How to dynamically load ESM in CJS

How to convert a CJS module to an ESM

How to create a CJS module

How to stream to an HTTP response

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑