npm run Explained

What npm run does, with an example.

Published November 10, 2026

npm run executes a script defined in the scripts section of package.json, such as build, test or dev. npm start and npm test are shortcuts.

How it works

Scripts run with node_modules/.bin on the PATH, so locally installed tools work without a global install.

Pass extra arguments after --.

Try it

npm run build
> app@1.0.0 build
> vite build
dist/ created

Practical examples

Define scripts

{ "scripts": { "dev": "vite", "build": "vite build", "test": "jest" } }

Run

npm run dev
npm test

Pass arguments

npm run test -- --watch

Chain

"prebuild": "rimraf dist", "build": "tsc", "postbuild": "echo done"

Common mistakes

  • Forgetting -- before extra flags.
  • Cross-platform shell syntax in scripts (use cross-env).
  • Long scripts that belong in files.

Key takeaways

  • npm run executes a script defined in the scripts section of package.json, such as build, test or dev.
  • Run npm help <command> for every option.
  • Commit package-lock.json.

FAQ

What are pre/post hooks?

prebuild and postbuild run automatically around build.

Does this work with yarn or pnpm?

They have equivalents with similar commands.

More JavaScript articles