Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Pass arguments to npm scripts

    Elijah ManorElijah Manor

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.

    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.

    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.

    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.

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