How to fix npm ERR! Unexpected end of JSON input in npm

npmBEGINNERMEDIUMUpdated Dec 6, 2025ยท 255 views

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
  1. Check file structure
  2. Check end of file
  3. Delete and regenerate package-lock.json
  4. Clear npm cache
npm โ€” error
# Count opening vs closing braces
grep -o "{" package.json | wc -l
grep -o "}" package.json | wc -l
# Should be equal
npm 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

  1. File truncated during save
  2. Missing closing braces or brackets
  3. Incomplete package-lock.json
  4. Corrupted npm cache
  5. Disk full during write
  6. Editor crashed while saving
  7. Incomplete git merge

How to fix "npm ERR! Unexpected end of JSON input"

1Check file structure

Verify brackets are balanced:

bash
# Count opening vs closing braces
grep -o "{" package.json | wc -l
grep -o "}" package.json | wc -l

# Should be equal
2Check end of file

View the file ending:

bash
tail -5 package.json

# Should end with:
# }

If truncated, the ending will look incomplete.

3Delete and regenerate package-lock.json

If lock file is corrupted:

bash
rm package-lock.json
npm install

This recreates a valid lock file.

4Clear npm cache

Fix corrupted cached packages:

bash
npm cache clean --force
rm -rf node_modules
npm install
5Restore from git

If file was valid before:

bash
# Check what changed
git diff package.json

# Restore previous version
git checkout HEAD -- package.json

# Or from specific commit
git checkout abc123 -- package.json
6Manually fix the file

If you know what's missing:

json
// 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