TExcelTAD
Home › Node.js › package.json & Scripts

package.json & Scripts

Every Node project has a package.json file at its root. It is the project's ID card: its name and version, which packages it needs, how Node should treat its files, and a set of handy scripts that anyone on the team can run.

A typical package.json

json
{
  "name": "todo-api",
  "version": "1.2.0",
  "description": "A small REST API for todo lists",
  "type": "module",
  "main": "src/server.js",
  "engines": { "node": ">=22" },
  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch --env-file=.env src/server.js",
    "test": "node --test",
    "lint": "eslint src"
  },
  "dependencies": {
    "express": "^5.1.0"
  },
  "devDependencies": {
    "eslint": "^9.20.0"
  },
  "license": "MIT"
}
Field Meaning
name, version identify the package (required if you publish it)
type "module" makes .js files ES modules; default is CommonJS
main / exports the entry point other code gets when it imports this package
engines which Node versions the project supports
scripts named shell commands run with npm run <name>
dependencies packages needed at run time
devDependencies packages needed only for development and tests
private true prevents accidental npm publish

Reading package.json from code

It's just JSON, so your program can read its own metadata — for example to print a --version:

node · try it yourself
const fs = require("node:fs");

fs.writeFileSync("package.json", JSON.stringify({
  name: "todo-api",
  version: "1.2.0",
  scripts: { start: "node server.js", test: "node --test" },
  dependencies: { express: "^5.1.0" },
}, null, 2));

const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
console.log(`${pkg.name} v${pkg.version}`);
console.log("scripts:", Object.keys(pkg.scripts).join(", "));
console.log("deps:", Object.entries(pkg.dependencies));

fs.unlinkSync("package.json");
todo-api v1.2.0
scripts: start, test
deps: [ [ 'express', '^5.1.0' ] ]

Running scripts

npm run <name> runs a script. A few names (start, test, stop, restart) have shortcuts that skip run:

bash · try it yourself
$ npm start
$ npm test
$ npm run lint
> todo-api@1.2.0 start
> node src/server.js

Server listening on http://localhost:3000

> todo-api@1.2.0 test
> node --test

ℹ tests 12
ℹ pass 12
ℹ fail 0

> todo-api@1.2.0 lint
> eslint src

Why use scripts instead of typing the command?

  • Everyone runs the same command with the same flags.
  • npm adds node_modules/.bin to the PATH, so locally installed tools like eslint work without npx.
  • CI systems and hosting platforms know to call npm test and npm start.

Pre and post scripts, and passing arguments

A script named pre<name> runs automatically before <name>, and post<name> after it. Use -- to forward extra arguments to the underlying command:

bash · try it yourself
$ npm test -- --test-name-pattern="login"
> todo-api@1.2.0 test
> node --test --test-name-pattern=login

ℹ tests 3
ℹ pass 3
ℹ fail 0

Tip: Since Node 22, node --run <script> runs a package.json script directly. It starts faster than npm run but skips pre/post hooks.

Scripts can see package.json too

When npm runs a script it exposes information through environment variables such as npm_package_name, npm_package_version and npm_lifecycle_event (the script's name). This playground fakes those variables so you can see how a script would use them:

node · try it yourself
// npm would set these when running `npm run build`
process.env.npm_package_name = "todo-api";
process.env.npm_package_version = "1.2.0";
process.env.npm_lifecycle_event = "build";

const { npm_package_name: name, npm_package_version: version,
        npm_lifecycle_event: script } = process.env;

console.log(`[${script}] building ${name}@${version}`);
[build] building todo-api@1.2.0

The exports field

Modern packages use exports instead of main to control exactly which files consumers may import:

json
{
  "name": "my-lib",
  "type": "module",
  "exports": {
    ".": "./src/index.js",
    "./utils": "./src/utils.js"
  }
}

Now import "my-lib" and import "my-lib/utils" work, but import "my-lib/src/secret.js" is refused. The same field can also define imports aliases ("imports": { "#db": "./src/db.js" }) for clean internal paths.

Test yourself

Which command runs the script named dev in package.json?

  1. npm dev
  2. npm run dev
  3. node dev
  4. npm exec dev

What does "type": "module" do?

  1. Marks the package as private
  2. Treats .js files in the package as ES modules
  3. Installs TypeScript
  4. Enables strict mode in CommonJS

If package.json has prebuild and build scripts, what does npm run build do?

  1. Runs only build
  2. Runs prebuild, then build
  3. Runs build, then prebuild
  4. Fails because of the duplicate name