Beginner LevelNode JS
How do npm package.json scripts and lifecycle hooks (pre/post scripts) operate?
❓ Interview Prompt: Explain npm scripts, running commands (`npm run build`), pre/post lifecycle hooks (`pretest`, `posttest`), and environment PATH resolution.
npm scripts execute terminal commands inside 'package.json':
Pre / Post Lifecycle Hooks:
Prefixing a script name with 'pre' or 'post' automatically executes that hook before or after the main script!
•
Running 'npm run test' automatically executes 'pretest' first, then 'test', then 'posttest'!
Code Implementation Example
json
{
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"postbuild": "echo Build Complete!",
"test": "node --test"
}
}💡Defining npm pre and post script lifecycle hooks.
🎯 Interview Key Takeaway
Use npm pre/post hooks to automate cleanup and build steps automatically before running main scripts.
#npm#scripts#lifecycle hooks#Beginner
Back to all Node JS Questions→