npm
Node Package Manager
In short
npm is Node.js's default package manager and the world's largest software registry; it downloads a project's dependencies and tracks their versions.
What is npm?
npm was created by Isaac Z. Schlueter in 2010 and ships with every installation of Node.js. It has two parts: a command-line tool that installs and manages packages, and the npm registry, an online store of millions of open-source JavaScript packages that anyone can publish to. It has been owned by GitHub since 2020.
A project lists its dependencies in a package.json file, together with scripts such as test and build. Running npm install downloads the packages, and the packages they depend on, into a node_modules folder and records the exact versions in package-lock.json, so every developer and server gets the same set. Versions follow semantic versioning, and ranges such as ^4.2.0 allow compatible updates.
npm run executes the scripts from package.json, and npx runs a package's command without installing it globally, for example to create a new project. Alternatives such as pnpm, Yarn and Bun use the same registry and package.json, but install packages differently, often faster or with less disk space.
A common misconception is that npm is only for Node.js on servers. Most frontend tools and libraries, such as React, Vite and TypeScript, are installed through it as well. Because installing a package can run its install scripts and pulls in many indirect dependencies, the registry has also been a target of supply chain attacks, so lockfiles and audits matter.
At a glance
Key takeaways
- npm is Node.js's package manager and the largest JavaScript package registry.
- package.json lists dependencies and scripts; package-lock.json pins exact versions.
- npm install fills node_modules; npm run executes scripts; npx runs package commands.
- pnpm, Yarn and Bun are alternatives that use the same registry.
- Lockfiles and audits help guard against supply chain attacks.
Example
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"test": "vitest"
},
"dependencies": {
"react": "^19.0.0"
},
"devDependencies": {
"vite": "^7.0.0",
"vitest": "^3.0.0"
}
}Readers ask
What is the difference between npm and npx?
npm installs and manages packages. npx runs a command from a package, downloading it temporarily if it isn't installed, which is handy for one-off tools such as project generators.
What is package-lock.json?
A file npm writes that records the exact version of every installed package, including indirect ones, so that installs on other machines produce the same result. It should be committed to version control.
What is the difference between npm, Yarn and pnpm?
All three install packages from the same registry using package.json. Yarn and pnpm were created to be faster or more efficient; pnpm, for example, stores each package version once on disk and links it into projects.
See also
- npm vs Yarnnpm ships with Node.js and installs into node_modules; Yarn is a separate client for the same registry, with its own lockfile and Plug'n'Play installs.
- npm vs pnpmnpm copies every package into each project's flat node_modules; pnpm keeps one copy in a shared store, links it in and blocks undeclared imports.
- Node.jsBackend & APIs, p. 35Node.js is an open-source JavaScript runtime that runs JavaScript outside the browser, most often to build web servers, APIs, and command-line tools.
- JavaScriptWeb Development, p. 29JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- Semantic VersioningVersion Control, p. 36Semantic versioning is a MAJOR.MINOR.PATCH numbering scheme in which each part signals whether a release breaks compatibility, adds features, or fixes bugs.
- Supply Chain AttackSecurity, p. 50A supply chain attack compromises software through something it relies on, like an open-source package, a build tool or an update server, not the app itself.
- MonorepoVersion Control, p. 31A monorepo is a single version control repository that holds the code for many projects, such as several apps, services, and shared libraries, managed together.
- BundlerWeb Development, p. 7A bundler is a build tool that combines JavaScript modules and their dependencies into a few optimized files that browsers can download and run quickly.
- Package ManagerProgramming Fundamentals, p. 47A package manager is a tool that installs, updates and removes software packages and their dependencies, resolving versions automatically.
- YarnWeb Development, p. 72Yarn is a JavaScript package manager, an alternative to npm, that installs dependencies from the npm registry and adds a lockfile, workspaces and Plug'n'Play.
- pnpmWeb Development, p. 39pnpm is a fast, disk-efficient JavaScript package manager that keeps one copy of each package version in a shared store and links it into projects.
Free · once a week
Software and AI, explained once a week
The week's software and AI news with every term explained, plus a term to learn and a quick quiz. A two-minute read.
See this week's issueAbout this page
Page history
Added a diagram: publishing, installing and running scripts with npm.
Changes that altered what this page says. Typo fixes are not listed.
Spotted a mistake or something missing on this page?Suggest an edit
How pages are made