Developer tools
npm and package.json
A program that downloads other people's code (packages) into your project and keeps track of which versions are where.
Why it exists
Almost every task in development has already been solved by someone: dates and time zones, server requests, tests, building a site. Without a package manager you'd copy that code by hand, hunt for updates across websites and remember who has which version. npm does it with one command: packages live in the shared registry at npmjs.com, the list you need is written in package.json, and any developer can rebuild the project with npm install.
Where it's used
- Every modern front-end project: React, Vue, Next.js and bundlers are installed with npm.
- Node.js servers: web frameworks, database drivers, logging.
- Developer tools: tests (
vitest), a linter (eslint), formatting (prettier) — indevDependencies. - Project commands:
npm run dev,npm run build,npm test— one entry point for anyone who opens the project.
Syntax
npm install name # a dependency the program needs to run
npm install -D name # only for development (tests, build)
npm install # install everything from package.json and the lockfile
npm run script-name # run a command from "scripts"Examples
const pkg = {
name: "byte-blog",
version: "1.0.0",
scripts: { dev: "next dev", test: "vitest run" },
dependencies: { dayjs: "^1.11.13" },
devDependencies: { vitest: "^3.2.4" },
};
Object.keys(pkg.dependencies); // → ["dayjs"]
pkg.scripts.test; // → "vitest run" — this is what npm test runsconst pkg = {
dependencies: { dayjs: "^1.11.13", react: "^19.0.0" },
devDependencies: { vitest: "^3.2.4", eslint: "^9.0.0" },
};
const all = { ...pkg.dependencies, ...pkg.devDependencies };
Object.keys(all).length; // → 4
"vitest" in pkg.dependencies; // → false — users don't need testspackage.json is JSON, so you can read it like an objectconst scripts = { dev: "next dev", build: "next build", test: "vitest run" };
// npm run build → runs the string from scripts
const command = (name) => scripts[name] ?? `Missing script: "${name}"`;
command("build"); // → "next build"
command("deploy"); // → "Missing script: \"deploy\""npm run does: looks the command up by nameResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
package.json is the project's passport: name, version, scripts and two lists of dependencies. dependencies holds what the program needs to run; devDependencies holds what only the developer needs.
The lists don't hold exact versions but ranges: ^1.11.13 means "any 1.x.x that is at least 1.11.13". On install npm asks the registry which versions exist and picks the newest one that fits — for every dependency, and for the dependencies of dependencies (transitive ones). The whole tree lands in the node_modules folder; identical packages are installed once, at the root, whenever possible.
So that a teammate doesn't get different versions a month later, npm writes the exact result to package-lock.json: which version of every package is installed and where it was downloaded from. npm install and npm ci rebuild the tree from that file. That's why package.json and the lockfile are kept with the code and node_modules isn't: it can always be rebuilt.
npm runs on Node.js — JavaScript outside the browser, with access to files and the network. Code from node_modules reaches your users only if the bundler finds it in your imports.
Traps
When to pick something else
If the task takes a couple of lines or a built-in method, write it yourself: "5".padStart(3, "0") instead of the left-pad package. Every dependency is someone else's code that you have to update and trust. A package pays off when the task is hard and already well solved: dates, cryptography, parsing Markdown.
Practice
Lessons where this appears
- Why a bundler, if the browser understands import?Why? · Chapter 24 · Modules: import and export
- Three days on a calendarTale · Chapter 25 · npm and packages
- Why is `node_modules` so heavy?Why? · Chapter 25 · npm and packages
- New versionLab · Chapter 25 · npm and packages
- The TyposquatterBoss · Chapter 25 · npm and packages
- The Great ConflictBoss · Chapter 28 · Git: the history of code