Pass arguments to npm scripts

Elijah Manor
InstructorElijah Manor
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 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.

egghead
egghead
~ 26 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today