Use a shorthand syntax for running multiple npm scripts with npm-run-all

Elijah Manor
InstructorElijah Manor

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

Running multiple scripts in series or in parallel can become very verbose. Using a tool such as npm-run-all can help reduce the amount of overhead you have to type in order to get the same behavior.

Instructor: [00:01] To clean up our test script, we can leverage a nice NPM package called npm-run-all. You can install it with npm-i-npm-run-all and -d to save it to your dev dependencies. Now, we can update our test script by running npm-run-all, which allows you to reference your NPM scripts by their name, so you don't have to prepend to run in front of them.

[00:28] By default, the scripts will be run in series, so you don't need the ampersands, either. Now, we can save our code and run the NPM tests again and see that the scripts run in series, just as they did before. If we wanted these to run in parallel, we could come back and we could add the --parallel flag.

[00:47] Let's go ahead and move the mocha command to right before the style-int, so that we can see it run in parallel. NPM-run-all purportedly terminates the process when hitting Ctrl-C in parallel mode, so there's no need to append the weight command. To test this, let's add the watch flag at the end of our mocha script, so we can verify that's working as expected.

[01:09] Now, if we run our tests again, the scripts will run, and you'll notice the tests were ran last, even though it was the second script showing the parallel feature. At this point, if we opened one of our unit tests and were to save the file, it would rerun the tests, and yes it did.

[01:27] If we hit Ctrl-C, the process will actually terminate. We can verify this by saving the unit test file again, and yes, it has been terminated.