package.json is the one file every JavaScript project has. npm, pnpm, Yarn and Bun all read it, and most of its fields come down to a few decisions.
Identity: name and version
{ "name": "@acme/widgets", "version": "1.4.0", "license": "MIT" }
- name: lowercase and URL-safe, optionally scoped (
@org/name). Required to publish. - version: semver. Bump the major for breaking changes, the minor for features and the patch for fixes.
For an app you never publish, add "private": true so npm publish refuses to run.
Module system: "type"
"type": "module" makes every .js file an ES module (import / export). Without it, .js files are CommonJS (require). Either way, .mjs is always ESM and .cjs is always CommonJS.
Entry points: the exports map
exports is the modern replacement for main. It lists exactly what consumers can import:
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./package.json": "./package.json"
}
- Put
typesfirst. Conditions are matched in order. - Anything not listed is private. Consumers can no longer deep-import
dist/internal.js. - Ship only an
importcondition if you only publish ESM.
scripts
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "vitest run",
"lint": "eslint .",
"prepublishOnly": "npm run build"
}
Run them with npm run build. test and start also work without run. Lifecycle names such as prepublishOnly or postinstall run automatically.
engines
"engines": { "node": ">=22" } documents the supported runtime. Node 20 reached end-of-life in April 2026, so new projects should target 22 or 24. npm only warns on a mismatch unless you set engine-strict.
The three dependency groups
| Field | Installed when | Example |
|---|---|---|
dependencies | always, at runtime | zod |
devDependencies | local dev and CI only | typescript, vitest |
peerDependencies | the host app provides it | react in a component library |
Publish only what you mean to
"files": ["dist"] is an allow-list for npm publish. Without it you can ship tests, fixtures, or worse, a stray .env. Check what will be published with npm pack --dry-run.
Security notes
postinstallscripts in your dependencies run on every machine that installs them. Review new packages, and consider--ignore-scriptsin CI.- Commit the lockfile so CI installs exactly what you tested.
- Run
npm auditor a registry scanner on every pull request.
Open the package.json template and ask the workspace to "add a CommonJS entry to the exports map" or "move typescript to devDependencies", then review the diff.