Skip to main content

How to Create a package.json with npm init

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every Node.js project has a package.json, a small JSON file that says what the project is called, how to run it and which packages it needs. npm init writes one for you, and with -y it does so without asking a single question.

Syntax​

Syntax
npm init [-y]
PartWhat it does
npm initCreates a package.json in the folder you're in. On its own, it asks for each field.
-yOptional, also spelled --yes. Skips the questions and uses the default for every field.

The Example​

The example creates a folder for a small project named greeting-app and gives it a package.json.

Prerequisites​

  • Node.js, which comes with npm. Check it with npm --version. This guide uses npm 12.2.3, which ships with Node.js 26.

Step 1: Create the Project Folder​

Terminal
mkdir greeting-app
cd greeting-app

How to Create a Folder in the Terminal with mkdir and How to Move into a Folder in the Terminal with cd explain these two commands. These and the next command are the same in zsh, bash and PowerShell.

Step 2: Run npm init​

Terminal
npm init -y
Output
Wrote to /Users/Shared/you/greeting-app/package.json:

{
"name": "greeting-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "commonjs"
}

npm prints where it wrote the file and what's in it.

A zsh terminal where mkdir greeting-app and cd greeting-app create and enter the folder, and npm init -y writes a package.json with the name greeting-app, version 1.0.0 and type commonjs.

What Each Field Means​

FieldDefaultWhat it's for
nameThe folder's nameThe package's name, which npm needs if you ever publish it.
version1.0.0The package's version.
descriptionEmptyOne line about the project.
mainindex.jsThe file other code loads when it uses this package.
scriptsA placeholder testCommands you run with npm run <name>.
keywordsEmptyWords that help people find a published package.
typecommonjsHow Node.js reads .js files: commonjs for require(), module for import.

npm 12 writes "type": "commonjs" into every new package.json. Code that uses import needs "module" instead, which How to Use ES Modules in a Node.js Project sets with one command.

Common Mistakes​

MistakeWhat happensDo this instead
Running npm init in the wrong folderThe package.json lands in that foldercd into the project first and check with pwd
Expecting npm init to install anythingIt only writes package.jsonInstall packages afterwards with npm install
Using import with the default "type": "commonjs"Node.js stops with SyntaxError: Cannot use import statement outside a moduleSet "type": "module"

FAQ​

Can I change the fields later?​

Yes. package.json is a plain text file, so you can edit it, or set one field from the terminal with npm pkg set <field>=<value>.

What happens without -y?​

npm asks for each field in turn: package name, version, description, entry point, test command, Git repository, keywords, author, license and type. It shows each default in parentheses, such as version: (1.0.0), and pressing Enter keeps it.

Does npm init overwrite an existing package.json?​

No. It reads the existing file and keeps the values you set. Running npm init -y on a package.json that only had a name and a description kept both and added the missing fields.

Conclusion​

npm init -y creates a package.json with npm's defaults in one step. The fields describe the project, and type decides whether its .js files use require() or import.

Additional Resources​