foundation
npm scripts
Use package scripts for repeatable dev, test, build, validation, environment, and lifecycle workflows.
Package scripts are the contract for how a repo is developed, tested, built, and run — they encode repeatable commands and hide toolchain details.
{
"scripts": {
"dev": "node --watch src/server.js",
"build": "tsc -p tsconfig.build.json",
"start": "node dist/server.js",
"test": "vitest run",
"lint": "eslint .",
"prestart": "npm run build"
}
}
`npm run` adds `node_modules/.bin` to PATH — local CLIs without global installs. Lifecycle hooks (`pre*`, `post*`) chain steps but can surprise CI if overused.
Cross-platform scripts: prefer small Node wrappers or tools like `cross-env` instead of shell-specific syntax unless documented for Unix-only dev.
On interviews: describe how you structure scripts for CI vs local dev, `prepare` for git hooks, and why `npm test` should be the single entry for verification.
Common pitfalls: business logic buried in bash one-liners; scripts that mutate global state; missing `CI=true` behavior branches.
The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.
Checklist:
- One obvious command per task (`test`, `build`, `dev`).
- Pin toolchain via devDependencies.
- Keep scripts idempotent for CI.
- Document env vars scripts expect.