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
{
"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:
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:
$ 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/.binto thePATH, so locally installed tools likeeslintwork withoutnpx. - CI systems and hosting platforms know to call
npm testandnpm 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:
$ 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 thannpm runbut 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:
// 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:
{
"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?
npm devnpm run devnode devnpm exec dev
What does "type": "module" do?
- Marks the package as private
- Treats
.jsfiles in the package as ES modules - Installs TypeScript
- Enables strict mode in CommonJS
If package.json has prebuild and build scripts, what does npm run build do?
- Runs only build
- Runs prebuild, then build
- Runs build, then prebuild
- Fails because of the duplicate name