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
- Getting Started with Svelte
- How to Serve Static Files with Nginx and Docker
- How to deploy a PHP app using Docker
- How to deploy a Deno app using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Creating a Twitter bot with Node.js
- Using Push.js to Display Web Browser Notifications