Skip to content
Back to all posts
Technology

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.

3 min read

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

json
{
  "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

bash
npm run lint -- --fix

The -- 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:

json
{
  "scripts": {
    "audit:content": "node scripts/audit-content.mjs"
  }
}

Run npm run audit:content after creating that file.

Chain scripts safely

json
{
  "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