How to create a package.json file?
© https://nodejs.org/en/

How to create a package.json file?

Generate a package.json file - manually or automatically

By Mario Kandut

Europe’s developer-focused job platform

Let companies apply to you

Developer-focused, salary and tech stack upfront.

Just one profile, no job applications!

This article is based on Node v16.14.0.

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

Create a package.json

💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.

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. 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.

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.

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.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

HeyNode, NPM

More node articles:

How to create a web server in Node.js

How to dynamically load ESM in CJS

How to convert a CJS module to an ESM

How to create a CJS module

How to stream to an HTTP response

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑