How to Create a package.json File
Table of Contents
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.
BASHnpm 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:
BASHThis 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!
- How to Install Node on Windows, macOS and Linux
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- How to build a Discord bot using TypeScript
- How to deploy a PHP app using Docker
- How to deploy an Express app using Docker
- How to Scrape the Web using Node.js and Puppeteer
- Getting User Location using JavaScript's Geolocation API
- Using Push.js to Display Web Browser Notifications
- Setting Up a Local Web Server using Node.js