How to fix npm ERR! Unexpected end of JSON input in npm
The "Unexpected end of JSON input" error occurs when npm receives truncated or incomplete JSON. This typically indicates a corrupted file, incomplete download, or prematurely closed brackets.
Quick fix
Fixes npm ERR! Unexpected end of JSON input
# Count opening vs closing braces
grep -o "{" package.json | wc -l
grep -o "}" package.json | wc -l
# Should be equal- Check file structure
- Check end of file
- Delete and regenerate package-lock.json
- Clear npm cache
# Count opening vs closing bracesgrep -o "{" package.json | wc -lgrep -o "}" package.json | wc -l# Should be equalnpm ERR! Unexpected end of JSON input
What this error means
This error means the JSON parser reached the end of the file or input while still expecting more data. JSON requires properly closed structures - every { needs a }, every [ needs a ], and strings need closing quotes. This commonly happens with truncated files, incomplete network responses, or files where closing brackets were accidentally deleted.
Typical symptoms
- Error mentions "end of JSON input"
- File appears incomplete
- Recent file editing or merge
- Network download was interrupted
- Cache corruption
- File system errors
Common causes
- File truncated during save
- Missing closing braces or brackets
- Incomplete package-lock.json
- Corrupted npm cache
- Disk full during write
- Editor crashed while saving
- Incomplete git merge
How to fix "npm ERR! Unexpected end of JSON input"
Verify brackets are balanced:
# Count opening vs closing braces
grep -o "{" package.json | wc -l
grep -o "}" package.json | wc -l
# Should be equalView the file ending:
tail -5 package.json
# Should end with:
# }If truncated, the ending will look incomplete.
If lock file is corrupted:
rm package-lock.json
npm installThis recreates a valid lock file.
Fix corrupted cached packages:
npm cache clean --force
rm -rf node_modules
npm installIf file was valid before:
# Check what changed
git diff package.json
# Restore previous version
git checkout HEAD -- package.json
# Or from specific commit
git checkout abc123 -- package.jsonIf you know what's missing:
// Truncated file might look like:
{
"name": "pkg",
"dependencies": {
"lodash": "^4
// Add missing content:
{
"name": "pkg",
"dependencies": {
"lodash": "^4.17.21"
}
}Advanced notes
Use version control for all JSON config files. Configure editor to create backup files. For CI/CD, always validate JSON files before operations. npm cache can become corrupted on disk errors - regular npm cache verify can help detect issues. Consider using atomic file writes in scripts that modify JSON.
Frequently asked questions
- What does "npm ERR! Unexpected end of JSON input" mean?
- This error means the JSON parser reached the end of the file or input while still expecting more data. JSON requires properly closed structures - every { needs a }, every [ needs a ], and strings need closing quotes. This commonly happens with truncated files, incomplete network responses, or files where closing brackets were accidentally deleted.
- How do I fix "npm ERR! Unexpected end of JSON input"?
- The "Unexpected end of JSON input" error occurs when npm receives truncated or incomplete JSON. This typically indicates a corrupted file, incomplete download, or prematurely closed brackets.
- What causes "npm ERR! Unexpected end of JSON input"?
- File truncated during save Missing closing braces or brackets Incomplete package-lock.json
- Is the "npm ERR! Unexpected end of JSON input" 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
- ๐
- ๐