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.


    Create a basic package.json file

    Elijah ManorElijah Manor

    We will walk through how to create a simple package.json file manually as well as create a default package.json file through npm with npm init -f. Then we will look at where the npm scripts live and how to run the default script that was created.



    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




    To create a package JSON file, we'll type npm init on the terminal. Npm will proceed to ask several questions about our project. Most of the questions have a default answer provided, for example, here, I could just press the enter key and it'll accept the current folder's name as the project's name.

    Since the project hasn't been published yet, I'll mark the version as 01.0As for the description, I'll summarize it as a tool to generate file size changes for the react library. The entry point maps the main property, which is the primary entry point to your program, we'll just keep it to index.js.

    If you knew what your test scripts should be at this point, you could provide it here, but we'll just use the default value now. If you've cloned this repo, it'll provide a default to that location, otherwise, you could provide your own. The keywords field could be helpful for people who want to find your project, and in this case, we'll say "react" and "file".

    As for the author, you could put your name or your business's name. I typically go with the MIT license as it's very permissive and has very few restrictions.

    At this point, npm will show you a preview of your package JSON and ask you to confirm if that's what you want. In this case, we'll say yes. We could open our package JSON file and we can verify that the data we entered is indeed in the file.

    Let's try something else. Let's remove the package JSON. If you don't want to answer all those questions, you can type the npm init and pass the --force flag to force all the default answers and not prompt you.

    You can also use the --yes flag that does the same thing or the shortcuts for -y and -f. If we pull up the package JSON file, you notice that it did generate a file for us with all the default values.

    Now that we have a package JSON file, we can start interacting with it. If you type npm run on the command line, it'll install all the scripts that npm knows about. In this case, you'll see that tests is the only one provided.

    To run the test script, we could run npm test or just npmt for short and it ran our test, which is just an echo saying that we don't have any tests.