Day 4 – npm and Package Management
Today's Focus
Master npm: understand package.json, dependency groups, semantic versioning, and how package-lock.json ensures reproducible installs.
Tasks
- Open
package.jsonand map every field:name,version,scripts,dependencies,devDependencies,peerDependencies,engines. Explain the purpose of each. Add anenginesfield restricting tonode >= 20. - Compare
dependenciesanddevDependencies: move any package used only in tests or build tooling todevDependencies. Confirm your app still compiles and runs. Explain why this matters for production Docker image size. - Study
package-lock.json: find a transitive dependency (one not in yourpackage.json) and trace which direct dependency introduced it. Check its version satisfies the semver range specified. - Understand semantic versioning: for
"vitest": "^2.1.0","~2.1.0", and"2.1.0"— write out exactly which version ranges npm would accept for each. Then pin a dependency to an exact version and explain when you would do this. - Run
npm ls --depth=0to see your direct dependency tree andnpm ls <package>to find why a specific transitive package is installed. - Add a
preparescript that runsnpm run buildautomatically afternpm install. Test it in a fresh clone. Discuss whyprepareruns on bothinstallandpublish.
Reading / Reference
- npm docs: package.json.
- Semantic Versioning 2.0.0 — the full spec is a short read.
- npm docs: package-lock.json.