How to fix npm ERR! package.json must have name in npm

npmBEGINNERMEDIUMUpdated Dec 6, 2025ยท 149 views

The "Missing name" error occurs when your package.json lacks the required "name" field. The name is mandatory for npm to identify and work with your package.

Quick fix

Fixes npm ERR! package.json must have name

{
  "name": "your-package-name",
  "version": "1.0.0"
}
  1. Add the name field
  2. Use npm init
  3. Check for JSON errors
  4. Restore from git
npm โ€” error
{
"name": "your-package-name",
"version": "1.0.0"
}
npm ERR! package.json must have name

What this error means

The name field is one of only two required fields in package.json (along with version). It identifies your package and is used for installation, publishing, and referencing. Without a name, npm cannot identify your package, cannot install it from the registry, and cannot distinguish it from other packages.

Typical symptoms

  • npm operations fail saying name is required
  • Fresh project created manually
  • Name field accidentally deleted
  • Merge conflict removed the field
  • Malformed JSON truncated the name
  • Template missing required fields

Common causes

  1. Created package.json manually without name
  2. Accidental deletion during editing
  3. Bad merge resolution
  4. Incomplete template
  5. JSON syntax error hiding the field
  6. Copy-pasted partial JSON

How to fix "npm ERR! package.json must have name"

1Add the name field

Include name in package.json:

json
{
  "name": "your-package-name",
  "version": "1.0.0"
}

Both name and version are required.

2Use npm init

Let npm create valid package.json:

bash
# Interactive
npm init

# Quick with defaults
npm init -y

This ensures all required fields exist.

3Check for JSON errors

The name might exist but be unparseable:

bash
# Validate JSON
node -e "console.log(JSON.parse(require('fs').readFileSync('package.json')))"

# Pretty print to check
cat package.json | npx json
4Restore from git

If accidentally deleted:

bash
# See what changed
git diff package.json

# Restore
git checkout HEAD -- package.json
5Fix merge conflicts

If name was lost in merge:

bash
# Check for conflict markers
grep -n "<<<<<<" package.json

# Manually resolve or:
git checkout --theirs package.json
# Then verify and re-add your changes
6Choose a good name

Name selection tips:

- Descriptive but concise
- Lowercase with hyphens
- Check availability: npm view desired-name
- Consider scope for org: @myorg/package

Advanced notes

For private packages not published to npm, name is still required but doesn't need to be unique. Private packages typically use scopes (@company/package). In monorepos, each package needs its own name. Workspaces reference packages by name, so consistency matters.

Frequently asked questions

What does "npm ERR! package.json must have name" mean?
json (along with version). It identifies your package and is used for installation, publishing, and referencing. Without a name, npm cannot identify your package, cannot install it from the registry, and cannot distinguish it from other packages.
How do I fix "npm ERR! package.json must have name"?
The "Missing name" error occurs when your package.json lacks the required "name" field. The name is mandatory for npm to identify and work with your package.
What causes "npm ERR! package.json must have name"?
Created package.json manually without name Accidental deletion during editing Bad merge resolution
Is the "npm ERR! package.json must have name" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading