Learning navigation

Code with Animation

npm Scripts & Lifecycle Hooks

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

Package Managers4 min readConcept 12 of 24

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)

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.

prebuild -> build -> postbuild
{
  "scripts": {
    "prebuild": "eslint .",
    "build": "tsc",
    "postbuild": "echo Done"
  }
}

Illustrative trace

> npm run prebuild
> eslint .

> npm run build
> tsc

> npm run postbuild
> echo Done
  1. 1

    prebuild

    Check the source

    eslint .
  2. 2

    build

    Compile the project

    tsc
  3. 3

    postbuild

    Run the final step

    echo Done

Local simulation of a successful npm script sequence with matching pre/post hooks. No commands execute. The example trace stays readable without replay; reduced motion shows the settled flow. A failed step can stop later hooks.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1With successful prebuild, build and postbuild scripts defined, what is their normal npm run build order?
  2. 2What does npm add to a script process so it can resolve local tool commands?
  3. 3Why should you consider dependency install hooks when reviewing an install policy?

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.

Done with this concept?

Mark it complete to track your progress. No login needed.