Chapter 17: Node package.json

1. What is package.json really?

package.json is the identity card and configuration file of every Node.js project.

It tells:

  • Who the project is (name, version, description)
  • How to run it (scripts)
  • What it needs to work (dependencies)
  • What tools are used only during development (devDependencies)
  • What kind of module system it uses (type: “module” or commonjs)
  • Which Node.js version it expects (engines)
  • Much more…

Almost every serious Node.js project has one — and you should never commit a project without it.

2. Creating package.json – the realistic ways

Way 1 – Quick & most common (recommended)

Bash

→ Creates a very basic package.json instantly

Way 2 – Interactive (you answer questions)

Bash

→ npm asks you many questions (you can press Enter to skip most)

Way 3 – Manual Just create an empty file called package.json and fill it yourself.

3. Real-world package.json – annotated line by line (2026 style)

This is a modern, realistic package.json for a medium-sized API project:

JSON

4. Most important fields – explained like a senior developer

Field Why it matters Common values / tips
name Unique identifier – used when publishing or referencing locally lowercase, kebab-case, no spaces
version Semantic versioning – controls updates & compatibility 1.0.0 → 1.1.0 (minor) → 2.0.0 (breaking)
private Prevents accidental npm publish true for almost all apps
type “module” = ESM, no field or “commonjs” = require Set “type”: “module” for new projects
scripts Custom commands – heart of daily workflow dev, start, build, test, lint…
dependencies Runtime packages – go to production express, prisma, zod, jsonwebtoken…
devDependencies Development-only tools – not sent to production typescript, eslint, vitest, nodemon, tsup…
engines Specifies minimum Node.js version “>=20.0.0” or “>=20.11.0 <21”
volta Pins exact node & npm versions (great for teams) Works with Volta tool – very popular
lint-staged Runs linters/formatters only on staged files (pre-commit) Used with husky

5. Common real-world patterns & tips (2026)

  • Use “private”: true → almost always for applications
  • Prefer “type”: “module” + ESM syntax for new work
  • Add engines field → prevents “it works on my machine” issues
  • Use volta or nvmrc to lock Node version in team
  • Keep devDependencies separate → reduces production bundle size
  • Use descriptive scripts names — npm run typecheck, npm run db:reset, etc.
  • Run npm audit regularly (security)
  • Use npm ci instead of npm install in CI/CD

Summary – Quick cheat sheet of must-know fields

JSON

Which direction would you like to go next?

  • Deep dive into scripts section (real-world examples)
  • How package-lock.json really works
  • Semantic versioning explained with real examples
  • Publishing your own package to npm
  • Using volta / nvm / corepack to manage versions
  • Difference between npm, yarn, pnpm in practice

Just tell me what feels most useful right now — I’ll continue with detailed examples. 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *