greenjam
v1.0.1
Published
Logs a message to the console when the package is installed.
Readme
install-logger
A tiny demo package that prints a message to the console when it's installed,
using npm's postinstall lifecycle hook.
How it works
package.json defines several install-time lifecycle hooks:
"scripts": {
"preinstall": "node lifecycle.js",
"install": "node lifecycle.js",
"postinstall": "node lifecycle.js",
"prepare": "node lifecycle.js"
}When the package is installed as a dependency, npm runs these hooks in order
(preinstall → install → postinstall, plus prepare for git/local/link
installs). lifecycle.js reads npm_lifecycle_event and console.logs which
hook fired.
Why postinstall might not run
Not every package manager runs dependency lifecycle scripts. Bun and pnpm block them by default for security and only run them for packages you explicitly trust:
- Bun: add the package to
trustedDependenciesin the installing project'spackage.json, then reinstall. Without it, none of these hooks run. - pnpm (v10+): approve with
pnpm approve-builds, or list the package underpnpm.onlyBuiltDependencies. - npm: runs them by default unless
--ignore-scriptsis set.
Try it locally (without publishing)
# from another folder
npm install /path/to/install-loggerYou'll see the install message printed.
Publishing
- Change
nameinpackage.jsonto something unique (or keep the@yourusernamescope and swap in your npm username). npm loginnpm publish --access public
