Skip to content
Latchkey

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

CommandDoes
npm ciClean, lockfile-exact install (CI)
npm installInstall + update lockfile
npm run <script>Run a package.json script
npm testShorthand for run test
npm exec / npx pkgRun a binary without installing
npm outdatedList stale deps

Lifecycle hooks

ScriptRuns
pre<name>Before <name>
post<name>After <name>
prepareAfter install, before publish
prepublishOnlyBefore publish only

Useful flags

FlagEffect
--workspacesRun across all workspaces
-w <pkg>Target one workspace
--if-presentSkip if script missing
--silentQuiet 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →