npm Scripts Cheat Sheet: Commands, Arguments, and Automation
A developer reference for package.json scripts, npm run, pre/post hooks, environment variables, and safe task automation.
TechSilo
Technology writer & developer
npm scripts turn repeated terminal commands into version-controlled tasks. They live in the scripts section of package.json, making common workflows discoverable to everyone working on the project.
Basic package.json example
{
"name": "sample-project",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint .",
"typecheck": "tsc --noEmit"
}
}These commands are examples: they only work when their dependencies are installed and their configuration is valid.
Frequently used commands
| Command | What it does |
| --- | --- |
| npm run | List defined scripts |
| npm run dev | Execute the dev script |
| npm run build | Execute the build script |
| npm start | Execute start |
| npm test | Execute test |
| npm run lint | Execute your project's lint task |
| npm ci | Install exact dependencies from a lockfile |
Unlike npm run, npm ci is an npm command, not a script. It is particularly useful in automated builds because it requires an existing compatible lockfile.
Pass arguments to your script
npm run lint -- --fixThe -- separates npm's arguments from the arguments that should be passed to the underlying script. Whether --fix is supported depends on the command the script runs.
For a custom Node script:
{
"scripts": {
"audit:content": "node scripts/audit-content.mjs"
}
}Run npm run audit:content after creating that file.
Chain scripts safely
{
"scripts": {
"check": "npm run typecheck && npm run lint",
"verify": "npm run check && npm run build"
}
}The && operator stops the chain if an earlier command fails on typical command shells. For complex cross-platform automation, use a dedicated Node script rather than depending on shell-specific syntax.
What runs automatically?
npm supports lifecycle script names, including prebuild and postbuild, which can run around the corresponding npm command. Treat lifecycle hooks carefully: they can execute during installs or builds and are not merely descriptive labels.
Keep secrets out of package.json scripts. Use environment variables supplied by your host or CI service; do not put tokens directly in committed commands.
Environment and portability
On macOS/Linux, NODE_ENV=production node server.js sets an environment variable inline. That syntax is not portable to ordinary Windows Command Prompt. Prefer defining the environment in your deployment platform or using a cross-platform helper when needed.
In npm scripts, executables installed in node_modules/.bin are added to the script's command path. That is why a locally installed eslint can be invoked without a global installation.
Debugging checklist
If you see Missing script, inspect package.json and verify your working directory. If a command is not found, install the correct dependency. If npm ci fails, confirm that package-lock.json matches the package manifest.
Use scripts for repeatable jobs, but prefer focused names like test:unit, check:types, and audit:links over vague aliases. For CLI troubleshooting practice, see the [Git Commands Cheat Sheet](/blog/git-commands-cheat-sheet-mr83laea).
Want to put this into practice?
Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.
Related blog posts
CSS Flexbox Cheat Sheet: Alignment, Wrapping, and Responsive Rows
A practical CSS Flexbox reference covering display:flex, flex-direction, gap, justify-content, align-items, flex-wrap, and flex shorthand.
Read postCSS Grid Cheat Sheet: Columns, Rows, Areas, and Responsive Layouts
A practical CSS Grid reference with repeat(), minmax(), auto-fit, alignment, named areas, and responsive layout examples.
Read postEnvironment Variable Management: Secure Secrets and Deployment Practices
Keep environment configuration safe across local development, CI, and production without leaking API keys into browser bundles or logs.
Read postPractical CORS Configuration Security Guide for APIs
Secure cross-origin browser requests with explicit origin allowlists, correct preflight handling, and credential rules.
Read postTypeScript Utility Types Cheat Sheet: Pick, Omit, Partial and More
Practical examples of TypeScript Partial, Required, Pick, Omit, Record, ReturnType, Awaited, Extract, and Exclude.
Read post