npm scripts Explained

What npm scripts does, with an example.

Published November 12, 2026

npm scripts are commands defined in package.json that you run with npm run <name>. They are the standard place for build, test, lint and dev workflows.

How it works

Special names (start, test, stop) work without run. pre and post prefixes run automatically before and after another script.

Scripts run in a shell with node_modules/.bin on the PATH.

Try it

npm run lint && npm test
> eslint .
> jest
Tests: 12 passed

Practical examples

Common set

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint .",
    "test": "jest",
    "check": "npm run lint && npm test"
  }
}

Run in parallel

npx npm-run-all --parallel dev:*

Pass flags

npm run test -- --coverage

Lifecycle hooks

"prepare": "husky", "prepublishOnly": "npm run build"

Common mistakes

  • Windows/Unix shell incompatibilities.
  • Long inline scripts.
  • Forgetting -- before arguments.

Key takeaways

  • npm scripts are commands defined in package.json that you run with npm run <name>.
  • Run npm help <command> for every option.
  • Commit package-lock.json.

FAQ

Cross-platform env vars?

Use cross-env (NODE_ENV=production works only on Unix shells).

Does this work with yarn or pnpm?

They have equivalents with similar commands.

More JavaScript articles