Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to NPM
Understanding package.json File

Understanding package.json File

NPMBy Muhammad Fareed
npmunderstandingpackage.jsonfile

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"
}
How package.json drives an npm projectpackage.json: Dependencies and scripts. npm install: Resolves versions. node_modules: Installed packages. Lock file: Exact versions recorded. npm run: Runs your scriptspackage.jsonDependencies and scriptsnpm installResolves versionsnode_modulesInstalled packagesLock fileExact versions recordednpm runRuns your scripts
Diagram: How package.json drives an npm project.

Identity fields

FieldMeaning
nameLowercase package name; required if you publish. Scoped names look like @org/pkg
versionSemantic version, MAJOR.MINOR.PATCH
descriptionShort summary shown in search results
keywordsWords that help people find the package
licenseThe license, for example MIT
repository, homepage, bugsLinks to the code, docs and issue tracker
privateSet 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 for import and require, and it hides internal files.
  • type: "module" makes .js files 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

FieldPurpose
dependenciesNeeded to run in production
devDependenciesNeeded only to build or test
peerDependenciesPackages your library expects the host project to provide, such as React for a component library
optionalDependenciesInstall failures are tolerated
overridesForce a version of a nested dependency, useful for security fixes

Version ranges

Written asAccepts
^1.2.31.2.3 up to, but not including, 2.0.0
~1.2.31.2.3 up to, but not including, 1.3.0
1.2.3Exactly that version
* or latestAnything; 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 when files is 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": true on 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.