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 → install →
postinstall → prepare. On npm publish the chain is prepublishOnly →
prepack → prepare → postpack → publish → postpublish.
Tips and notes
- Put build output that consumers need in
prepare— it runs on publish and on git-dependency installs. - Use
prepublishOnlyfor tests and lint gates that should run only at publish. postversionis the conventional place to push tags afternpm version.- pnpm v7+ skips dependency install hooks for safety; allow-list any you trust.