If you find yourself waiting for your build to finish before tests can run, you’re likely invoking npm scripts sequentially. Automate npm scripts with npm-run-all to run parallel build and test commands, cutting CI time in half and keeping your workflow fluid.
Automate npm scripts with npm-run-all to run parallel build and test commands
Understanding npm‑run‑all
npm‑run‑all is a tiny utility that lets you orchestrate multiple npm scripts in a single command. It works with any version of Node and integrates seamlessly into existing package.json scripts.
Why use it?
- Simplifies complex orchestration without extra tooling.
- Runs tasks in parallel when you request it.
- Preserves order for dependent steps.
Step‑by‑step: Running Build and Test in Parallel
Imagine you have two scripts: scripts: { build: webpack --mode production, test: jest --coverage } that compile assets and run tests. Instead of waiting for npm run build to finish before starting npm test, you can fire them off together.
Define the scripts
In package.json add a scripts section like:
scripts: { build: webpack --mode production, test: jest --coverage }
Launch them together
Install the package globally or locally:
npm install –save-dev npm-run-all
Then run:
npx npm-run-all –parallel build test
This spawns both processes simultaneously, freeing up your terminal.
- Install
npm-run-allas a dev dependency. - Add your individual scripts to
package.json. - Create a new script that calls
npm-run-all --parallel <script1> <script2>. - Run the new script in CI or locally.
Handling Errors and Fail‑Fast
When tasks run in parallel, a failure in one process can be masked. Use the --continueOnError flag only if you want to collect all results, otherwise let the first error abort the run.
- Exit on first failure – default behavior; stops other tasks.
- Capture individual output – useful for debugging.
- Use separate log files – prevents interleaved console output.
Practical Trade‑offs and When Not to Use
Parallel execution isn’t a silver bullet. Consider these trade‑offs before refactoring all your scripts.
Debugging complexity
Interleaved logs can make it harder to trace which step failed.
Resource contention
Running many heavy tasks at once may overload your machine or CI runner.
Order‑dependent steps
If a script must wait for another’s output, keep them sequential.
Real‑world Example: A Full CI Script
Below is a typical CI entry point that combines linting, testing, and building in parallel:
scripts: { ci: npm-run-all –parallel lint test build }
Run it with npm run ci and watch the pipeline shrink from minutes to seconds.
Conclusion
By adopting npm-run-all you can automate npm scripts with npm-run-all to run parallel build and test commands, dramatically improving developer productivity. Give it a try in your next project and measure the time saved — your CI pipeline will thank you.