Home

Run JavaScript Script Before Another Script

Automatically run scripts with NPM/Yarn before or after another script.

If you want to run a script before another one completes, you can do this by prepending pre to the script name. NPM (or Yarn) will automatically run the script.

Suppose I have a script called test that runs my test suite. But before I do that, I want to make sure a script called build runs first. I could have a configuration like this:

pacakge.json

{
"scripts": {
"build": "...",
"pretest": "build",
"test": "..."
}
}

Now when I run this command:

npm run test

The pretest script will automatically be run first, and in the example above, that means the build script automatically runs before the test script.

Note: Use post as the prefix to run a script after another script.

Let's Connect

Keep Reading

How I Begin New JavaScript Projects

These are the first steps I take when I start a new JavaScript project.

May 26, 2021

Decreased Jekyll Build Times 5x with a Custom Asset Pipeline

My team and I ditched the jekyll-assets gem for a homegrown asset pipeline and decreased build times by a factor of five. This is how we did it.

Jan 10, 2019

10 Angular Tools to Build Creative Web Apps

To help the developers build robust and high-quality web apps, Angular offers plenty of useful tools.

Harikrishna Kundariya
Jun 30, 2022