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
| Capability | What it does |
|---|---|
| Installing packages | Installs external libraries (npm install express) |
| Publishing packages | Lets you publish your own package to the public npm registry |
| Managing dependencies | Tracks versions, stores them in package.json |
| Running scripts | Lets you run commands from the scripts section |
| Caching | Stores downloaded packages locally, speeding up reinstalls |
Example npm commands
| Command | Purpose |
|---|---|
npm init | Creates package.json |
npm install (or just npm i) | Installs all dependencies from package.json |
npm install express | Installs a specific package |
npm install --save-dev nodemon | Installs a package as a dev dependency |
npm uninstall express | Removes a package |
npm update | Updates dependencies |
npm run <script> | Runs a command from "scripts" in package.json |
npm publish | Publishes 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
my-app/
├── node_modules/
│ ├── express/
│ ├── chalk/
│ ├── lodash/
│ └── ...
├── package.json
├── package-lock.json
└── index.jsEach installed package is a folder inside node_modules.
How npm and node_modules relate
- When you install a package:
npm install express- 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
npm init -y # create package.json
npm install express # install the libraryAfter that, the structure:
my-app/
├── node_modules/
│ └── express/
├── package.json
└── package-lock.jsonImportant details
Local vs global packages
| Type | Command | Where it's installed |
|---|---|---|
| Local | npm install express | into ./node_modules of the current project |
| Global | npm install -g nodemon | into 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:
npm installand npm recreates node_modules from scratch.
Summary
| Concept | Description |
|---|---|
| npm | The package manager for Node.js (installs, updates, publishes libraries) |
| node_modules | The folder where installed packages physically live |
| package.json | Describes a project's dependencies |
| package-lock.json | Pins exact package versions for stability |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.