NPM — how to use pre & post hooks in 1 minute

QJ Li
2 min readApr 29, 2018

--

npm scripts is awesome

Maybe you are new to npm scripts, maybe you have been using it for a while but you do not know about it has pre and post hooks for the npm scripts, now I am going to show you how useful they are.

Before you know about pre and post hooks, suppose you are a fancy front end dev and you have high standard of your code and you would always run linting before running your tests.

$npm run lint && npm test

But sometimes you forgot about it. Yep, we all do, no matter how awesome we are, we are not robot at the end of the day.

After you know about pre and post hooks for npm scripts, you will do the following, and pretest script will always be executed before test script being run. posttest will always be executed after test script being run.

"scripts": {
"lint": "eslint",
"test": "jest",
"pretest": "npm run lint",
"posttest": "npm run build"
"build": "webpack"
}

Cool, I guess you would like these hooks and use them in your workflow tomorrow and be thankful for these awesome hooks npm provides!

--

--

QJ Li

Coding | Lead Myself | Family | Books | Personal CI / CD Daily