All academies

npm and Package Management

package.json, dependencies, scripts, and semantic versioning.

16 minLevel 5 of 20

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.

The npm lifecycle at a glance
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 versions

package.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:

  • name and version — identify the package
  • dependencies — packages your code needs at runtime
  • devDependencies — packages only needed while developing (test runners, linters)
  • scripts — named shell commands you run with npm run <name>
  • main / exports — the entry point other code gets when it requires your package
  • type — set to "module" to make .js files ES Modules
A typical package.json
{
  "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.x
  • 4.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 ci installs *exactly* what the lockfile says (and fails if it conflicts with package.json) — the right command for CI pipelines
  • npm install updates the lockfile when you add or upgrade packages

Never edit the lockfile by hand; npm manages it.

Local vs CI installs
# On your machine - resolves ranges, may update the lockfile:
npm install

# In CI - exact, clean, reproducible install from the lockfile:
npm ci

npm 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.

Scripts in action
# 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 questions

Question 1 of 5

Score: 0

Multiple choice

What is the difference between dependencies and devDependencies?