QA
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→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?