npm and Package Management
package.json, dependencies, scripts, and semantic versioning.
What npm is
npm is two things:
- A registry — the world's largest collection of open-source JavaScript packages (millions of them), hosted at registry.npmjs.org
- A command-line tool — bundled with Node.js, used to install packages, run scripts, and publish your own packages
Instead of writing everything from scratch, you install battle-tested packages: a web framework, a date library, a test runner. npm downloads them into a node_modules folder in your project, where require() and import can find them.
npm init -y # create a package.json with defaults
npm install express # add a dependency
npm install -D vitest # add a dev-only dependency
npm run test # run the "test" script from package.json
npm outdated # see which dependencies have newer versionspackage.json: your project's manifest
Every npm project has a package.json at its root. It records the project's name, version, dependencies, and scripts. Create one with npm init -y.
The most important fields:
nameandversion— identify the packagedependencies— packages your code needs at runtimedevDependencies— packages only needed while developing (test runners, linters)scripts— named shell commands you run withnpm run <name>main/exports— the entry point other code gets when it requires your packagetype— set to"module"to make.jsfiles ES Modules
{
"name": "my-api",
"version": "1.0.0",
"type": "commonjs",
"scripts": {
"start": "node server.js",
"dev": "node --watch server.js",
"test": "node --test"
},
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"vitest": "^1.6.0"
}
}Semantic versioning (semver)
npm packages use semantic versioning: MAJOR.MINOR.PATCH, like 4.19.2.
- MAJOR — breaking changes; upgrading may require code changes
- MINOR — new features, backwards compatible
- PATCH — bug fixes only
Version *ranges* in package.json control what npm may install:
^4.19.2(caret, the default) — any 4.x.x that is at least 4.19.2; never a new major~4.19.2(tilde) — only patch updates: 4.19.x4.19.2(exact) — that version and nothing else
The caret's promise relies on package authors following semver — which is why lockfiles exist.
package-lock.json and reproducible installs
Ranges like ^4.19.2 mean two people running npm install a month apart could get different versions. The package-lock.json file solves this: it records the *exact* version of every package (including dependencies of dependencies) that was installed.
- Commit the lockfile to git — it makes installs reproducible for teammates and CI
npm ciinstalls *exactly* what the lockfile says (and fails if it conflicts with package.json) — the right command for CI pipelinesnpm installupdates the lockfile when you add or upgrade packages
Never edit the lockfile by hand; npm manages it.
# On your machine - resolves ranges, may update the lockfile:
npm install
# In CI - exact, clean, reproducible install from the lockfile:
npm cinpm scripts and npx
The scripts field turns long commands into short, memorable names. npm run dev looks up scripts.dev and executes it with node_modules/.bin on the PATH — so locally installed tools work without global installs.
A few script names are special: npm start and npm test work without the word run.
npx runs a package's command directly. If the package is installed locally it uses that; otherwise it downloads a temporary copy, runs it, and cleans up. Great for one-off tools: npx create-next-app, npx serve.
# package.json scripts:
# "dev": "node --watch server.js"
# "test": "node --test"
# "lint": "eslint src/"
npm run dev # long-lived dev server with auto-restart
npm test # special name - no "run" needed
npm run lint # uses the eslint from node_modules/.bin
npx cowsay "one-off tools without installing"Check your understanding
5 questionsQuestion 1 of 5
Score: 0