How to create a package.json file?
Tags: node, npm, javascript
Published: 2021-06-08 19:38:56

---
title: How to create a package.json file?
published: true
date: 2021-03-13 00:00:00 UTC
tags: node, npm, javascript
canonical_url: https://www.mariokandut.com/how-to-create-a-package-json-file-node/
---

Creating a `package.json` file is typically the first step in a Node.js project, and it has to be available if you want to add dependencies via npm in your project. There are many app scaffolding packages available, like `create-react-app`, which do this for you. This blog post is about how to create a `package.json` from scratch. Have a look at this article - [What is the package.json](https://www.mariokandut.com/what-is-package-json-and-how-to-configure/)

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#create-a-packagejson)Create a package.json

The easiest way to create a package.json is:

- Enter the root folder of your project
- Run `npm init`
- Fill out the prompts to create your `package.json`

Just follow these steps.

1. Create a folder for your project.
2. Enter the folder you just created with `cd FOLDER_NAME` and run `npm init`. You should see a prompt like this:

```
➜ test npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (test)
```

1. Fill out the prompts for fields in your package.json. Either accept default, or type a new value. The package.json fields you'll be prompted for are `name`, `version`, `description`, `main`, `test command`, `git repository`, `keywords`, `author`, and `license`. It's okay to accept the defaults to get a package.json generated and be able to install dependencies. All fields are optional, if you don't publish your package to the NPM registry. If you do intend to publish your package, there are several requirements and in general you should fill out the prompts. You can learn more about the different fields about the package.json in this article - [The Basics of Package.json](https://www.mariokandut.com/what-is-package-json-and-how-to-configure/). The only real requirement (for now) is that the package.json is valid JSON.
2. Review the preview of the `package.json` after answering/accepting defaults of the prompts and accept (yes) or decline (no). The output should look something like this (accepted defaults):

```
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes)
```

1. **🎉 Tada. 🎉** You've successfully created a `package.json` and can now install dependencies for your project using `npm install <package>`. You can always manually edit your package.json file in your editor, if you want to change some values.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#accepting-the-defaults)Accepting the defaults

The npm CLI will try to infer defaults from the folder you ran npm init from. If you are about to accept the defaults generated by npm cli, you can also generate a package.json without being asked for input. Just run `npm init -y` to generate a package.json and automatically accept all defaults. The generated file will be shown on the command line.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Create a `package.json` with `npm init` in the project directory.
- If you don't want to publish to the NPM registry, the default values can be accepted automatically with `npm init -y`.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[HeyNode](https://heynode.com/tutorial/create-packagejson-file), [NPM](https://docs.npmjs.com/cli/v7/commands/npm-init/)