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


    Use package.json variables in npm scripts

    Elijah ManorElijah Manor

    In this lesson we will show that you can leverage values that you already have provided in your package.json file such as the name and version keys. You may want to use these in your npm scripts as you create folders and file names.



    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:02 MPM has package level variables you could use in your scripts such as name, version, etc. You could look at all of MPM's variables by running MPM run MB, and then we'll grab for just the ones that start with MPM_package.

    00:15 Then we'll pipe those to less. As you'll see, there's tons of variables and we could search for just the version which is the one we're about to use. Let's update our scripts so when we build it'll create a folder for our current version. We'll start with our prebuild script.

    00:33 Where we list the public folder we'll use the variable MPM package_version which in this case we'll resolve to public/01.0Note the dollar sign signifies that we're referencing a variable. Then in our build:HTML we'll also append the dollar sign MPM package_version. Let's go ahead and save that.

    00:57 Then we'll update our build:CSS script and our build:JSscript. Now if we save our file and go back to the terminal we could run MPM run build and we'll see the prebuild clean up the public folder, and each of the build:HTML build:CSS and build:JSscripts output their contents to the public/01.0folder. And that's it.