How to Create a package.json File

Updated onbyAlan Morel
How to Create a package.json File

When you install Node, you also get npm, which is the official package manager for Node.

Node uses a file called package.json that lives in the room of your project to store information about your project, which includes any dependencies needed from npm.

In this post, we'll learn how you can create a package.json file for your project to initialize a new Node project.

Creating a package.json file

The recommended way to create a package.json file is to use the npm init command in the root of your project.

BASH
npm init

This will open a CLI prompt that will ask you a few questions to help fill in your new package.json file.

It starts off like this:

BASH
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:

It will ask you for things like the name of your project, version, description, entry point, test command, git repository, keywords, author, and license.

You can just hit enter to accept the default value or you can type in your own value if you want to change it.

Once you finish with the questions, you'll be prompted to save the file.

After you have saved it, you should see a new file in your project called package.json.

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

From here, you can install any dependencies you need by typing npm install in the root of your project, and it will automatically alter the package.json file to include the dependencies, or you can manually edit the file yourself, then run npm install to install the dependencies.

Conclusion

Every Node project uses a package.json file to keep track of the dependencies and other information.

If you're starting a brand new Node project, you can use npm init to create a package.json file to get you started.

Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.