# package.json Anatomy & Fields

> Master the core manifest fields, module entry points, CLI command bindings, runtime engine constraints, and tarball inclusion rules of package.json.

Subject: Package Managers | Lesson 2 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/package-json-anatomy
Previous: [What is a Package Manager?](https://codewithanimation.in/package-managers/what-is-a-package-manager.md) | Next: [The "exports" Field & ESM vs CJS](https://codewithanimation.in/package-managers/module-exports-and-entry-points.md)

## What is package.json?

`package.json` is a JSON manifest file residing at the root of a Node.js project. It records essential project metadata, binary entry points, script shortcuts, target runtime engines, and publication rules in a single declarative file.

## Why is package.json Essential?

The manifest records package metadata, entry points, scripts and dependency requirements in one place. Publication filters help control the payload, but inspect the actual packed file list before publishing.

## How to Configure package.json

1. **Initialize Manifest**: Run `npm init -y` to generate a standard default `package.json` file.

2. **Configure Module Type**: Add `"type": "module"` if writing modern ES Modules with `import`/`export` syntax.

3. **Set Entry & Binary**: Point `"main": "./dist/index.js"` to compiled code and specify `"bin": { "my-tool": "./bin/cli.js" }` for CLI tools.

4. **Safeguard Publication**: Set `"private": true` for internal applications, or add `"files": ["dist", "README.md"]` for open-source library releases.

## Common mistake: Invalid JSON syntax and improper relative file paths

package.json must be valid JSON: quote property names and strings correctly and avoid trailing commas. Entry paths must resolve to the intended files; main can use a path such as dist/index.js without a leading ./, while exports targets have their own stricter rules. Executable bin scripts need the documented interpreter setup.

## Tip: Use `npm pkg set` for safe CLI updates and explicitly restrict `files`

Use npm pkg set to update manifest fields through npm. Define an intentional files list for publication, then inspect npm pack --dry-run output. Inclusion rules and matched directories still need review; a files array is not by itself proof that no secret can be published.

## Try it: Inspect package.json Fields

Select a field chip below to inspect its role, runtime behavior, npm CLI defaults, and code syntax.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/package-json-anatomy

## Check yourself

### Question 1

Which package.json setting tells npm to refuse publishing this application?

- A. "version": "0.0.0"
- B. "main": "index.js"
- C. "scripts": {}
- D. "private": true

Correct answer: D

The private flag prevents npm publication. A private registry URL alone does not prevent accidentally publishing a package.

- Why not A: A version number does not disable publication.
- Why not B: main declares an entry point, not publication permission.
- Why not C: An empty scripts object does not block publishing.

### Question 2

You want a command named study and a package payload containing dist files. Which fields serve those roles?

- A. bin maps the command; files selects included paths
- B. files maps the command; bin selects included paths
- C. engines maps the command; scripts selects included paths
- D. dependencies maps the command; private selects included paths

Correct answer: A

bin maps executable names to scripts. files controls which paths are included in the package, alongside npm's inclusion rules.

- Why not B: These two fields have the opposite roles.
- Why not C: engines describes runtime compatibility; scripts declares tasks.
- Why not D: Neither field defines this command/payload combination.

### Question 3

A package declares "type": "module". How does Node interpret its ordinary .js files?

- A. As TypeScript source files
- B. As ES modules within that package scope
- C. As CommonJS regardless of syntax
- D. As globally installed executable commands

Correct answer: B

The nearest parent package.json controls the .js module type. Explicit .cjs and .mjs extensions keep their own meanings.

- Why not A: type does not add TypeScript compilation.
- Why not C: module selects ES-module interpretation for .js.
- Why not D: Module format does not install executables.

## Remember this

- name and version identify publishable packages. Requirements for private workspace metadata depend on the manager and workflow; not every private package must be publishable.
- main defines a package entry point; it is not inherently CommonJS-only. The type field controls how Node interprets .js files within the package scope.
- `bin` maps executable CLI command aliases to script paths (which must start with `#!/usr/bin/env node`), and `files` whitelists what gets published.
- Use `npm pkg set` to mutate fields safely without syntax errors, and set `"private": true` to prevent accidental public deployment.
