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

Already subscribed? Sign In

Autoplay

    Configuring npm package.json scripts

    Ben ClinkinbeardBen Clinkinbeard

    With a node package manager's (npm) package.json script property, you can preconfigure common tasks like running unit tests with npm $SCRIPT_NAME

    javascriptJavaScript
    nodeNode.js
    Code

    Code

    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
    Transcript

    Transcript

    Man 1: 00:01 When npm initialized our package.json file for us it created this scripts field and within it entered a test entry which right now is just going to echo out an error message. The scripts field is extremely useful and a recommended way for you to provide workflow scripts for your module for people that are using it. Right now, if we were just to say, npm test, it's going to spit out that error message that we see defined here. But making sure that your module's test can be run with npm test is a big part of being a good module author.

    00:43 To demonstrate this, I've just defined the functionality of our module to just be a simple function that will convert a string to upper case. I've written a simple mocha test here that requires that file. This ./ just means that it's going to look in the current directory and default to a file named "index." We just have a simple test here that just says expect up-hello to equal hello in uppercase. We can see that that does work. That test passes if we just run mochatest.js.

    01:20 We can see that one is passing. But in order for somebody using your module to know that you're using Mocha, that the file is called test.js, that's a little bit more work than they should really have to do. What it's better to do, is to go in here and update this test field with that exact command. If we put that into our test field we can then come down here and say, npm test, and it's going to run that exact command for us so we can see that our tests are passing.

    01:58 Now somebody can install your module, run npm test, and they don't need to know or care about what sort of testing library you're using or how things are set up. They know that your tests are passing. There's also a shortcut. You can do npm t. It's the same thing. Test and a few other ones are sort of special fields in the npm world. You can run them directly like we just saw with npm test. You can also add custom ones here.

    02:32 If we wanted to add something like a check version. We'll make that run Mocha version. If we then save this we can then run this custom script by doing npm run checkversion. Any time that you run a custom script you can just do it with npm run and the name. It will look for that name within your scripts field.

    Discuss

    Discuss