Understanding package.json File
Short answer: package.json is the manifest of a JavaScript project. It names the project, lists its dependencies, defines scripts and tells Node and npm how to load your code. Create one with npm init -y.
A complete example
{
"name": "my-app",
"version": "1.2.0",
"description": "A small example application",
"type": "module",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"test": "jest"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"jest": "^29.7.0"
},
"engines": { "node": ">=18" },
"license": "MIT"
}
Identity fields
| Field | Meaning |
|---|---|
name | Lowercase package name; required if you publish. Scoped names look like @org/pkg |
version | Semantic version, MAJOR.MINOR.PATCH |
description | Short summary shown in search results |
keywords | Words that help people find the package |
license | The license, for example MIT |
repository, homepage, bugs | Links to the code, docs and issue tracker |
private | Set to true to prevent accidental publishing |
Entry points and module type
main: the file loaded when someone imports your package (the classic field).exports: the modern way to define entry points, including separate files forimportandrequire, and it hides internal files.type:"module"makes.jsfiles ES modules; omit it or use"commonjs"for CommonJS.bin: maps command names to scripts, so your package can ship a command-line tool.types: the TypeScript declaration entry point.
Scripts
The scripts object holds named commands run with npm run name. start and test work without run. See npm scripts for hooks and patterns.
Dependency fields
| Field | Purpose |
|---|---|
dependencies | Needed to run in production |
devDependencies | Needed only to build or test |
peerDependencies | Packages your library expects the host project to provide, such as React for a component library |
optionalDependencies | Install failures are tolerated |
overrides | Force a version of a nested dependency, useful for security fixes |
Version ranges
| Written as | Accepts |
|---|---|
^1.2.3 | 1.2.3 up to, but not including, 2.0.0 |
~1.2.3 | 1.2.3 up to, but not including, 1.3.0 |
1.2.3 | Exactly that version |
* or latest | Anything; avoid in real projects |
The lock file, package-lock.json, records the exact versions chosen so every machine installs the same tree.
Controlling what gets published
files: a whitelist of files and folders to include in the published package..npmignore: files to leave out (used whenfilesis absent).engines: the supported Node version range.
Follow the whole process in how to publish an npm package.
Monorepos
The workspaces field lets one repository hold several packages that share a single install, for example "workspaces": ["packages/*"].
Common mistakes
- Putting build tools in
dependencies, which bloats production installs. - Editing the lock file by hand.
- Invalid JSON: no comments and no trailing commas are allowed.
- Forgetting
"private": trueon an internal project.
Next steps
Start from the beginning with what npm is, or set up a project with how to set up a Node.js project.
