How to fix npm ERR! package.json must have name in npm
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"
}- Add the name field
- Use npm init
- Check for JSON errors
- Restore from git
{"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
- Created package.json manually without name
- Accidental deletion during editing
- Bad merge resolution
- Incomplete template
- JSON syntax error hiding the field
- Copy-pasted partial JSON
How to fix "npm ERR! package.json must have name"
Include name in package.json:
{
"name": "your-package-name",
"version": "1.0.0"
}Both name and version are required.
Let npm create valid package.json:
# Interactive
npm init
# Quick with defaults
npm init -yThis ensures all required fields exist.
The name might exist but be unparseable:
# Validate JSON
node -e "console.log(JSON.parse(require('fs').readFileSync('package.json')))"
# Pretty print to check
cat package.json | npx jsonIf accidentally deleted:
# See what changed
git diff package.json
# Restore
git checkout HEAD -- package.jsonIf name was lost in merge:
# Check for conflict markers
grep -n "<<<<<<" package.json
# Manually resolve or:
git checkout --theirs package.json
# Then verify and re-add your changesName 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
- ๐npm Official Documentation
- ๐
- ๐