npm Scripts Cheat Sheet: Commands, Lifecycle & CI Flags
npm commands, lifecycle scripts, and the CI-friendly flags in one reference.
Install, run, and version with npm - plus the pre/post script lifecycle.
Core commands
| Command | Does |
|---|---|
| npm ci | Clean, lockfile-exact install (CI) |
| npm install | Install + update lockfile |
| npm run <script> | Run a package.json script |
| npm test | Shorthand for run test |
| npm exec / npx pkg | Run a binary without installing |
| npm outdated | List stale deps |
Lifecycle hooks
| Script | Runs |
|---|---|
| pre<name> | Before <name> |
| post<name> | After <name> |
| prepare | After install, before publish |
| prepublishOnly | Before publish only |
Useful flags
| Flag | Effect |
|---|---|
| --workspaces | Run across all workspaces |
| -w <pkg> | Target one workspace |
| --if-present | Skip if script missing |
| --silent | Quiet output |
Example
package.json
{
"scripts": {
"build": "tsc && vite build",
"prebuild": "npm run lint",
"test": "vitest run",
"ci": "npm run build && npm test"
}
}Key takeaways
- Use npm ci in pipelines - it is faster and fails on lockfile drift.
- prebuild/postbuild hooks fire automatically around build.
- --if-present avoids failing when a workspace lacks a script.
Related guides
pnpm Cheat Sheet: Install, Workspaces & FiltersA pnpm cheat sheet - install, add, run, frozen-lockfile, workspace filters, and the store commands that make…
Yarn Cheat Sheet: Classic & Berry CommandsA Yarn cheat sheet - install, add, run, workspaces, and the immutable-install flag, covering both Yarn Classi…
Semantic Versioning Cheat Sheet: SemVer Rules & RangesA semantic versioning cheat sheet - MAJOR.MINOR.PATCH rules, prerelease and build metadata, and the npm range…