npm/pnpm Lifecycle Hooks Reference

All npm lifecycle script names in execution order for install, pack and publish.

Reference for npm and pnpm package.json lifecycle hooks — preinstall, install, postinstall, prepare, prepack, prepublishOnly, version — with trigger command and exact execution order, plus a live command filter.

What is the difference between prepare and prepublishOnly?

prepublishOnly runs only on npm publish, never on local installs, so it is ideal for publish-only guards like running tests. prepare runs on publish, on local npm install with no args, and on git-dependency installs, making it the right place for build steps that consumers need.

npm lifecycle hooks in execution order

npm runs a defined set of lifecycle scripts at known points during install, pack, publish and version operations. Knowing the exact order — and which command triggers which hook — lets you place build steps, guards and release automation in the right package.json field. This reference groups every hook by its triggering command and shows the precise sequence, with a live filter.

How it works

Each script is a named field under scripts in package.json. For any script x, npm automatically wraps it with prex and postx when those exist:

{
  "scripts": {
    "preinstall": "node check-node-version.js",
    "prepare": "tsc -p .",
    "prepublishOnly": "npm test",
    "postversion": "git push --follow-tags"
  }
}

On npm install the order is preinstall → dependency install → installpostinstallprepare. On npm publish the chain is prepublishOnlyprepackpreparepostpackpublishpostpublish.

Tips and notes

  • Put build output that consumers need in prepare — it runs on publish and on git-dependency installs.
  • Use prepublishOnly for tests and lint gates that should run only at publish.
  • postversion is the conventional place to push tags after npm version.
  • pnpm v7+ skips dependency install hooks for safety; allow-list any you trust.