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 ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error