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

3 Ways to Render Server-Side Components with Eleventy

While Eleventy doesn't appear to be built for today's component-driven landscape, here are three approaches we can take to get closer.

Jan 10, 2021

Use Dynamic Property Maps over Switch Case Statements

An everyday JavaScript pattern to avoid clunky switch-case statements and unnecessary if conditionals.

May 19, 2022

Compile ES6 Code with Gulp and Babel, Part 1

In the first of five parts on compiling multiple ES6 files into a minified bundle, you will learn how to setup the project and build a simple implementation.

Dec 17, 2018