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.
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# Count opening vs closing bracesgrep -o "{" package.json | wc -lgrep -o "}" package.json | wc -l# Should be equalnpm ERR! Unexpected end of JSON input
On this page
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.
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"
}
}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.