Skip to content

package.json

A package.json file is the manifest at the root of a JavaScript project that records its name, version, dependencies, and scripts, along with settings that tell runtimes how to load its code.

The npm command creates one with npm init, and other package managers such as pnpm and Yarn read the same format. The file must be valid JSON, so it can’t hold comments or trailing commas. The name and version fields are required only for a package that gets published to the npm registry.

Package managers read the fields that describe the project. The dependencies and devDependencies fields list the packages it needs, with a version range for each. The scripts field maps names such as test to shell commands that npm run executes.

Node.js reads the fields that control module loading. The type field decides how .js files load. The value "module" makes them ES modules, and "commonjs" makes them CommonJS. Without the field, Node.js treats them as CommonJS unless they contain ES module syntax. The exports field lists a package’s public entry points and blocks imports of any other file inside it.

Example

Say a team moves an invoice tool to ES modules by adding "type": "module" to its package.json:

Language: JSON Filename: package.json
{
  "name": "invoice-tool",
  "version": "1.0.0",
  "type": "module"
}

One older file in the project still calls require() to load a built-in module:

Language: JavaScript Filename: main.js
const { join } = require("node:path");

console.log(`Folder: ${join("invoices", "2026")}`);

Running the file now fails before it prints anything:

Language: Shell
$ node main.js
ReferenceError: require is not defined in ES module scope, you can use import instead

The new type field makes Node.js load every .js file under the manifest as an ES module, and require() doesn’t exist in ES module scope. Renaming the file to main.cjs keeps it CommonJS, because the .cjs and .mjs extensions override the type field.

Pick a type value and a file extension to see how Node.js loads the same require() call and what running it prints:

Interactive diagram — enable JavaScript to view.
Developer with a clipboard approving package boxes as an npm 12 checkpoint machine stamps install scripts as blocked

Tutorial

npm v12 and npm Supply Chain Attacks: What It Blocks

See how npm v12 blocks install scripts you haven't approved. Compare npm 11 vs 12 on Node 26, and learn which npm supply chain attacks still get through.

intermediate tools

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 24, 2026