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.


    Add comments to your npm scripts

    Elijah ManorElijah Manor

    The need for comments in your package.json file becomes desirable the more and more npm scripts you start to define. At first glance this might seem like a show stopper since JSON does not support comments, but there are ways around this limitation that we will discuss.



    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




    The more and more scripts you have, there can become the need for documentation. However, since the package.JSON file is JSON, that's somewhat limiting.

    However, there are a couple techniques you can use to make it work. One is to use a double slash as your key, which lets you put whatever you want as a description and npm will ignore it.

    In this case, we'll say run, build, and serve. Then, we can come down to our test and do the same thing. Run our Mocha unit tests, and then, we'll also do it for our linting.

    Lint, our JavaScript, and Sass. Now, the downside of using the slash slash technique is that when you npm run, and we'll pipe it through Less, it'll only print out the last comment, since they share the same key, which is slash slash.

    You see it has lint or JavaScript in Sass, but all the other comments are gone. The main benefit of the slash slash technique is to document your actual package.JSON file itself and not the output when you run npm run from the command line. Another technique we could use is to start our script with a shell comment.

    Then here, we'll put an emoji, because it's fun, and then, we'll say run, build, and serve. Then, we'll separate it with a new line and a tab character.

    Now, let's quickly make the changes for our other scripts. We'll have a comment, and a zap, and then, we'll say run our Mocha unit tests, separated by a new line and a tab character.

    We'll save that and come over to our linting. Start with a comment and then have a traffic emoji, and say lint our JavaScript in Sass, separated by a new line and a tab character.

    Now when we run npm run again, piped through Less, we'll see a description of our script and the script itself. You'll notice that it's nicely indented.

    A con of the second technique I showed you is that it clutters up your package.JSON file and obscures your actual script. Outside of providing your scripts elsewhere, these are your primary options for providing comments inside your package.JSON file.