Pass arguments to npm scripts

Elijah Manor
InstructorElijah Manor

Share this video with your friends

Send Tweet
Published 7 years ago
Updated 5 years ago

Often times you’ll have variations that you’ll want to make to your npm scripts and repeating yourself is inefficient and verbose. Instead you can create a base script and pass arguments into it from another script.

[00:01] If you wanted to create a watch script for your tasks, you might be tempted to copy the test script and rename it to watch:test and then at the end add the dash dash watch flag.

[00:12] That would work. However, it would be somewhat brittle, because if you ever made a change to your test script in the future, you'd also need to make a change to your watch test script. That's not optimal.

[00:22] Instead, what you could do is to call up the script that you want, which is MPM test, and then add the special dash dash option which MPM recognizes as a way to parse additional arguments to your script.

[00:36] In this case, we want to parse the dash dash watch flag to our test script. If we save our code and open up our terminal, we could run MPM run watch:test and it'll kick up the test script, parsing along the dash dash watch flag which will watch our file system for any changes to our code, and run our tests.

[00:56] If I open up one of our test files and go ahead and save it, sure enough, it'll re-run our tests.