# npm Scripts & Lifecycle Hooks

> Automate your workflows with package.json scripts and powerful pre/post lifecycle hooks.

Subject: Package Managers | Lesson 12 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/npm-scripts-and-lifecycle
Previous: [npx (Package Runner)](https://codewithanimation.in/package-managers/npx-and-package-exec.md) | Next: [Yarn & Plug'n'Play (PnP / Zero Installs)](https://codewithanimation.in/package-managers/yarn-and-plug-n-play.md)

## The scripts object

The `scripts` object in your `package.json` is a powerful task runner. It allows you to define custom command-line aliases for your project, such as `"start": "node server.js"` or `"test": "jest --passWithNoTests"`.

You execute these custom scripts by typing `npm run <script-name>` in your terminal.

## The hidden $PATH magic

The biggest advantage of npm scripts is that npm temporarily alters your system's `$PATH` environment variable during execution.

Before running the script, npm prepends `node_modules/.bin/` to the `$PATH`. This means your script can simply call `"build": "tsc"` instead of typing out the full path `"build": "./node_modules/.bin/tsc"`.

Once the script finishes, the `$PATH` is restored, keeping your global environment perfectly clean.

## Lifecycle Hooks

npm scripts support automatic execution pipelines via **lifecycle hooks**. If you prefix any script name with `pre` or `post`, npm will automatically run them before or after the main script.

For example, if you have `"prebuild": "eslint ."`, `"build": "tsc"`, and `"postbuild": "echo Done"`, running `npm run build` will sequentially execute:

1. `prebuild` (runs the linter)

2. `build` (compiles the code)

3. `postbuild` (prints success)

## Common mistake: The postinstall security risk

Dependency installation can execute lifecycle hooks when allowed by the manager and its script policy. Do not assume that every downloaded package always runs all hooks under every configuration.

Install hooks can legitimately perform setup or native builds, but untrusted hooks can also execute unwanted code with the installing process's permissions. Blocking scripts can break required setup and does not sandbox later imports.

## Try it: Lifecycle Pipeline Simulator

Trigger the `npm run build` command below. Watch how npm automatically intercepts the command to run `prebuild` and `postbuild` hooks in the correct sequence.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npm-scripts-and-lifecycle

## Check yourself

### Question 1

With successful prebuild, build and postbuild scripts defined, what is their normal npm run build order?

- A. postbuild, then build, then prebuild
- B. prebuild, then build, then postbuild
- C. All three must run concurrently
- D. Only postbuild runs

Correct answer: B

npm recognizes matching pre/post names around a user-defined script. A failure can stop the sequence before later steps.

- Why not A: That reverses the defined hook ordering.
- Why not C: These lifecycle steps are ordered, not inherently parallel.
- Why not D: The requested build and its matching hooks participate.

### Question 2

What does npm add to a script process so it can resolve local tool commands?

- A. A new dependency entry for every command
- B. A copy of the tool inside package-lock.json
- C. Local node_modules/.bin entries in PATH
- D. A remote shell on the package registry

Correct answer: C

npm scripts can invoke project-installed executables by name. This process environment change does not permanently install those tools globally.

- Why not A: Executing a script does not declare new packages.
- Why not B: The lockfile contains metadata, not executable source copies.
- Why not D: Scripts are not sent to the registry for execution.

### Question 3

Why should you consider dependency install hooks when reviewing an install policy?

- A. Every downloaded archive always runs all hooks under every manager
- B. ignore-scripts prevents all future code execution by the package
- C. A postinstall hook proves the package is malicious
- D. They can execute package code; blocking them can also disable required setup

Correct answer: D

Install-script policy is a tradeoff. Some dependencies need setup or native builds, while untrusted hooks can run unwanted code. Behavior depends on the manager and configuration.

- Why not A: Managers and script policies differ.
- Why not B: It does not sandbox later imports or commands.
- Why not C: Legitimate packages can use hooks for necessary setup.

## Remember this

- npm scripts allow you to define command-line aliases for your project tasks.
- npm temporarily augments the `$PATH` to include `node_modules/.bin`, allowing direct access to local binaries.
- Prefixing a script with `pre` or `post` creates automatic lifecycle pipelines (e.g., `prebuild` -> `build` -> `postbuild`).
- Install hooks can execute package code when allowed; consider both trust and required setup when choosing a script policy.
