Skip to main content

What is npm?

What npm is

npm stands for

Node Package Manager - the package manager for Node.js.

In other words:

npm is a tool that lets you install, update, remove and manage libraries (packages) in Node.js projects.

Main npm functions

CapabilityWhat it does
Installing packagesInstalls external libraries (npm install express)
Publishing packagesLets you publish your own package to the public npm registry
Managing dependenciesTracks versions, stores them in package.json
Running scriptsLets you run commands from the scripts section
CachingStores downloaded packages locally, speeding up reinstalls

Example npm commands

CommandPurpose
npm initCreates package.json
npm install (or just npm i)Installs all dependencies from package.json
npm install expressInstalls a specific package
npm install --save-dev nodemonInstalls a package as a dev dependency
npm uninstall expressRemoves a package
npm updateUpdates dependencies
npm run <script>Runs a command from "scripts" in package.json
npm publishPublishes your own package to the npm registry

Where packages are stored after installing

All packages installed via npm land in the node_modules/ folder at the project root.

What node_modules is

node_modules is the directory where npm (or pnpm, yarn) installs all of a project's dependencies.

Example project structure

javascript
my-app/ ├── node_modules/ │ ├── express/ │ ├── chalk/ │ ├── lodash/ │ └── ... ├── package.json ├── package-lock.json └── index.js

Each installed package is a folder inside node_modules.

How npm and node_modules relate

  1. When you install a package:
javascript
npm install express
  1. npm:
  • looks up the package in the online npm registry (https://www.npmjs.com),
  • downloads it and all of its dependencies,
  • places everything in the node_modules/ folder,
  • records the name and version in package.json.

What package-lock.json is

After installing, npm creates a package-lock.json file:

  • it pins the exact versions of every installed package and its sub-dependencies;
  • it guarantees an identical install for every developer on the project;
  • it speeds up repeat installs.

Example process

javascript
npm init -y # create package.json npm install express # install the library

After that, the structure:

javascript
my-app/ ├── node_modules/ │ └── express/ ├── package.json └── package-lock.json

Important details

Local vs global packages

TypeCommandWhere it's installed
Localnpm install expressinto ./node_modules of the current project
Globalnpm install -g nodemoninto npm's external system directory

Why node_modules is needed

  • It is the physical storage for all of a project's dependencies.
  • Without it, the project won't run if dependencies aren't installed.

Why node_modules is not committed to GitHub

The folder can take up hundreds of megabytes. Instead, only these are added to the repository:

  • package.json, the list of dependencies;
  • package-lock.json, the exact versions.

When cloning a project, it's enough to run:

javascript
npm install

and npm recreates node_modules from scratch.

Summary

ConceptDescription
npmThe package manager for Node.js (installs, updates, publishes libraries)
node_modulesThe folder where installed packages physically live
package.jsonDescribes a project's dependencies
package-lock.jsonPins exact package versions for stability

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.