Understanding package.json
Complete field-by-field reference for package.json, including name, version, main, module, exports, type, scripts, dependencies, engines, and workspaces.
The package.json file is the central manifest for every Node.js project. It defines metadata, dependencies, scripts, and module resolution rules.
Anatomy of a Modern package.json
{
"name": "my-awesome-app",
"version": "1.0.0",
"description": "Production REST API with Express and TypeScript",
"private": true,
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
"test": "vitest"
},
"dependencies": {
"express": "^4.21.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^22.7.4",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"vitest": "^2.1.1"
},
"engines": {
"node": ">=20.0.0",
"pnpm": ">=9.0.0"
}
}Core Fields Explained
1. type ("module" vs CommonJS)
"type": "module": Treats all.jsfiles as ES Modules (import/export).- Omitted or
"commonjs": Treats.jsfiles as CommonJS (require/module.exports).
2. private
- Setting
"private": trueprevents accidental publication of proprietary or internal applications to the public npm registry.
3. exports (Subpath Exports)
Modern Node.js replacement for main. Controls exactly which entrypoints consumers can import:
"exports": {
".": "./dist/index.js",
"./utils": "./dist/utils/index.js"
}4. engines
Declares the supported runtime versions for your project:
"engines": {
"node": ">=20.0.0"
}Related Guides
pnpm: Fast, Disk Space Efficient Package Manager
Complete guide to pnpm, hard links, content-addressable storage, strict non-flat node_modules layout, and preventing phantom dependencies.
Runtime Dependencies (`dependencies`)
Guide to runtime dependencies in Node.js, when to install packages into dependencies, dependency resolution, and bundle size considerations.