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.
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.
npm notice access token expired or revoked. Please try logging in again.
Token has expired - npm authentication failure
npm ERR! code EAI_AGAIN
How to fix "EAI_AGAIN" in npm
npm error code E403 npm error 403 Forbidden - PUT https://registry.npmjs.org/<package>
How to fix 'E403 Forbidden' error in npm
npm ERR! code EUSAGE npm ERR! Usage error
How to fix "npm ERR! code EUSAGE" in Node.js projects
npm ERR! code E401 npm ERR! 401 Unauthorized
How to fix "E401 Unauthorized" in npm