Skip to the page

Modules and packages · mid

package.json, semver and lockfiles

package.json describes a package: its module type, its public entry points ("exports"), the Node versions it supports, its scripts and three kinds of dependencies, while the lockfile pins every exact version so each install is the same.

The notes

What you must know

  • dependencies (needed at run time), devDependencies (build and test only), peerDependencies (the host app must provide it, e.g. react for a component library).
  • Semver ranges: ^1.4.2 allows any 1.x.y from 1.4.2 up; ~1.4.2 allows 1.4.x only; for 0.x versions ^0.4.2 allows only 0.4.x (a 0.x minor bump may break).
  • package-lock.json pins the whole dependency tree. Commit it. Use npm ci in CI and Docker: a clean, exact install that fails if the lockfile and package.json disagree.
  • "exports" lists the public entry points and blocks deep imports of other files (ERR_PACKAGE_PATH_NOT_EXPORTED). Conditional exports ("import", "require", "types", "default") let one package serve ESM and CommonJS users. "main" is the older single entry.
  • "engines": { "node": ">=22" } documents supported versions; "type" sets how .js files load; "scripts" run with npm run or node --run (faster; skips pre/post hooks).
  • Supply chain: real npm packages have been hijacked to publish malware. Pin with the lockfile, review new dependencies, run npm audit, and be careful with install scripts.

The red pen

Common traps: mistake → fix

  • Mistake: ^ means 'always the latest version'

    Fix: ^ stays inside the same major version (and the same minor for 0.x).

  • Mistake: npm install in CI is fine

    Fix: npm ci gives exact, repeatable installs from the lockfile.

  • Mistake: "exports" is only about ESM

    Fix: it also hides internal files from deep imports.

Easy to hard

Questions interviewers ask, easy to hard

  1. dependencies vs devDependencies vs peerDependencies?
  2. What does ^ mean in a version range? And ~?
  3. Why commit package-lock.json? npm install vs npm ci?
  4. How would you publish a library that works for both import and require users?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview