TExcelTAD

package.json Field by Field

package.json is two documents in one file. For an application it is a build manifest: which dependencies to install and which scripts to run. For a library it is also the published metadata that every consumer's package manager and bundler reads. This lesson walks through the fields that matter, grouped by who reads them.

A realistic manifest

json
{
  "name": "@acme/units",
  "version": "2.3.0",
  "description": "Parse and format durations and byte sizes",
  "license": "MIT",
  "type": "module",
  "exports": {
    ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js", "require": "./dist/index.cjs" },
    "./package.json": "./package.json"
  },
  "main": "./dist/index.cjs",
  "types": "./dist/index.d.ts",
  "bin": { "units": "./bin/units.js" },
  "files": ["dist", "bin"],
  "sideEffects": false,
  "engines": { "node": ">=20" },
  "packageManager": "pnpm@10.34.7",
  "scripts": { "build": "rollup -c", "test": "node --test", "prepublishOnly": "npm run build" },
  "dependencies": { "ms": "^2.1.3" },
  "devDependencies": { "rollup": "^4.65.0", "typescript": "~5.9.3" },
  "peerDependencies": { "react": "^18 || ^19" },
  "peerDependenciesMeta": { "react": { "optional": true } }
}
Group Fields Read by
Identity name, version, description, license, repository, keywords the registry, npm view, search
Entry points type, exports, main, module, types, bin Node, bundlers, TypeScript, npm (for bin links)
What gets published files, .npmignore, private npm pack / npm publish
Dependencies dependencies, devDependencies, peerDependencies, optionalDependencies, bundleDependencies, overrides the package manager
Tooling scripts, engines, packageManager, workspaces, sideEffects, allowScripts npm, Corepack, bundlers

A few rules worth memorising:

  • name is lowercase, URL-safe and at most 214 characters; @scope/name puts it in a namespace you (or your organisation) own.
  • version must be valid semver. The pair name@version is immutable once published.
  • private: true makes npm publish refuse. Put it on applications and monorepo roots.
  • files is an allow-list; package.json, README and LICENSE are always included.
  • sideEffects: false tells bundlers that importing a module and not using it can be dropped (see Tree Shaking).

Editing it from the command line

npm pkg reads and writes fields without opening an editor, which is handy in scripts and CI. Dots address nested keys:

bash · try it yourself
mkdir -p units && cd units
npm init -y > /dev/null
npm pkg set name=@acme/units version=2.3.0 type=module
npm pkg set 'engines.node=>=20' 'files[]=dist' 'files[]=bin'
npm pkg set bin.units=./bin/units.js
npm pkg delete main description
npm pkg get name version engines
npm pkg get files bin
npm pkg fix
cat package.json
{
  "name": "@acme/units",
  "version": "2.3.0",
  "engines": {
    "node": ">=20"
  }
}
{
  "files": [
    "dist",
    "bin"
  ],
  "bin": {
    "units": "./bin/units.js"
  }
}
npm warn package-json @acme/units@2.3.0 No bin file found at bin/units.js
{
  "name": "@acme/units",
  "version": "2.3.0",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "engines": {
    "node": ">=20"
  },
  "files": [
    "dist",
    "bin"
  ],
  "bin": {
    "units": "bin/units.js"
  }
}

npm pkg set 'files[]=dist' appends to an array. npm pkg fix normalises the manifest the way npm publish would (here it dropped the ./ prefix in bin), and on the way it warned that bin/units.js doesn't exist yet: a useful check before you publish a broken command.

What files really publishes

The quickest way to see what consumers will receive is a dry-run pack. Anything not listed in files stays behind, including tests and source:

bash · try it yourself
mkdir -p units/{src,dist,bin,test} && cd units
cat > package.json <<'JSON'
{ "name": "@acme/units", "version": "2.3.0", "files": ["dist", "bin"], "bin": { "units": "bin/units.js" } }
JSON
echo 'export const ms = 1;' > src/index.js
echo 'export const ms = 1;' > dist/index.js
printf '#!/usr/bin/env node\nconsole.log("units");\n' > bin/units.js
echo 'test' > test/units.test.js
echo '# units' > README.md
npm pack --dry-run 2>&1 | sed -n '/Tarball Contents/,/Tarball Details/p'
npm notice Tarball Contents
npm notice 8B README.md
npm notice 42B bin/units.js
npm notice 21B dist/index.js
npm notice 108B package.json
npm notice Tarball Details

engines and packageManager

engines.node documents the Node versions you support. By default npm only warns when it doesn't match; set engine-strict=true in .npmrc to make it an error. packageManager ("pnpm@10.34.7") is read by Corepack, which then runs exactly that package-manager version for the project; see Yarn Classic, Yarn Berry & Corepack.

bash · try it yourself
mkdir -p old && cd old
echo '{ "name": "old", "version": "1.0.0", "engines": { "node": ">=26" } }' > package.json
npm install 2>&1 | grep EBADENGINE | head -1
npm install --engine-strict 2>&1 | grep -E 'code|notsup' | head -2
npm warn EBADENGINE Unsupported engine {
npm error code EBADENGINE
npm error notsup Not compatible with your version of node/npm: old@1.0.0

Test yourself

Which field limits which files end up in the published tarball?

  1. main
  2. files
  3. exports
  4. bin

What does "private": true do?

  1. Hides the package from npm ls
  2. Makes npm publish refuse to publish the package
  3. Encrypts the tarball
  4. Installs it only for the current user

By default, what does npm do when engines.node doesn't match the running Node?

  1. Refuses to install
  2. Prints an EBADENGINE warning and continues
  3. Downloads the right Node
  4. Nothing at all