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

Already subscribed? Sign In


    Create a bash script to replace a complex npm script

    Elijah ManorElijah Manor

    In this lesson we will look at pulling out complex npm scripts into their own external bash scripts. This ends up simplifying your package.json file and abstracts the complexity into another file.

    react-nativeReact Native


    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


    00:01 If your MPM scripts get out of hand, you could always decide to split up your script into external script vowels. For example, we'll convert two sets of our MPM scripts into shell scripts. We'll start with our tests.

    00:14 First, we'll copy our pre-test and test scripts, then, come over and create a new folder called scripts. Inside it, we'll add a file called test.sh. Inside our file, we're going to add a shebang; which is poundbang/user bin env bash, to signify that this is a bash script.

    00:37 We'll paste our script from our package json file and then do a little formatting. We'll save our file and go back to our package json. Here, we could delete our pretest, and inside of our test, we'll change it to say .scripts/test.sh.

    00:55 Before we could actually run our test, what we need to do is go to our scripts folder and change the permissions on that file so that we could execute it. Now, we could come back and run MPMT and it should run our tests.

    01:14 Now, let's convert our build scripts. We'll come down and come over to where the build happens, we'll copy these files. We'll come over and create a file for it called build.sh. Again, we'll need our shebang, poundbang/user bin env bash. We'll paste in our scripts.

    01:36 In this case, we won't need the first one. Remove the keys. Take off the quotes at the end. Save that. Again, we'll have to go to our terminal and change the permissions. If I wanted to change them all recursively, I could say, ch mode -r, for recursive, 777.

    02:07 Before we run our build, let's add some echo statements so we could see what's going on. We'll say echo building, then, we say echo finish building. We'll come to our package json, we'll delete most of the builds.

    02:23 For the main build, we'll say scripts build sh. We'll come to the terminal, now, we could say MPM run build. You'll see at echo it says building, then it actually built the files, and then, it says finished building.

    02:42 If we wanted to continue building the MPM scripts into other shell scripts, we could, but I'll leave that up to your imagination. This ends up being a simple and fine solution if you feel things are getting out of hand with your MPM scripts.