Create a basic package.json file

Elijah Manor
InstructorElijah Manor

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

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.

[00:00] 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.

[00:15] 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.

[00:33] 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".

[00:49] 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.

[00:58] 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.

[01:12] 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.

[01:23] 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.

[01:37] 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.

[01:48] 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.