How to fix npm ERR! Unexpected token in JSON at position in npm

npmBEGINNERMEDIUMUpdated Dec 6, 2025ยท 233 views

The "Unexpected token" JSON error occurs when npm encounters an invalid character while parsing package.json. This typically indicates a syntax error at the reported position.

Quick fix

Fixes npm ERR! Unexpected token in JSON at position

# Count to position in file
head -c 234 package.json | tail -c 20

# Or use Node
node -e "const fs=require('fs'); const c=fs.readFileSync('package.json','utf8'); console.log(c.substring(230,240))"
  1. Find the exact location
  2. Check for hidden characters
  3. Remove BOM if present
  4. Recreate the file
npm โ€” error
# Count to position in file
head -c 234 package.json | tail -c 20
# Or use Node
node -e "const fs=require('fs'); const c=fs.readFileSync('package.json','utf8'); console.log(c.substring(230,240))"
npm ERR! Unexpected token in JSON at position

What this error means

This error provides specific information about where JSON parsing failed. The "unexpected token" is the character that the parser didn't expect at that position. Common unexpected tokens include: } (extra closing brace), , (trailing comma), ' (single quote), or even invisible characters. The position number tells you exactly where in the file the error occurred, though it counts from the start of the file including whitespace.

Typical symptoms

  • Error shows specific position number
  • Error shows the unexpected character
  • JSON looks correct visually
  • Position seems wrong for visible content
  • Error after copy-pasting content
  • File edited on different OS

Common causes

  1. Invisible Unicode characters
  2. BOM (Byte Order Mark) at start of file
  3. CRLF vs LF line ending issues
  4. Copy-paste from word processor
  5. Smart quotes instead of straight quotes
  6. Zero-width spaces or joiners
  7. Tab/space mixing issues

How to fix "npm ERR! Unexpected token in JSON at position"

1Find the exact location

Use the position to locate the error:

bash
# Count to position in file
head -c 234 package.json | tail -c 20

# Or use Node
node -e "const fs=require('fs'); const c=fs.readFileSync('package.json','utf8'); console.log(c.substring(230,240))"
2Check for hidden characters

Reveal invisible characters:

bash
# Show all characters including hidden
cat -A package.json

# Or use hexdump
hexdump -C package.json | head

Look for unexpected byte sequences.

3Remove BOM if present

Check and remove BOM:

bash
# Check for BOM (EF BB BF)
file package.json
head -c 3 package.json | xxd

# Remove BOM
sed -i '1s/^\xEF\xBB\xBF//' package.json
4Recreate the file

Create a clean version:

bash
# Parse and rewrite
node -e "const p=require('./package.json'); console.log(JSON.stringify(p,null,2))" > package.json.new
mv package.json.new package.json

Or manually copy content to new file.

5Fix smart quotes

Replace smart quotes with regular:

bash
# Using sed
sed -i "s/[\u201c\u201d]/\"/g" package.json
sed -i "s/[\u2018\u2019]/'/g" package.json
6Use editor to fix

Open in VS Code:

1. File shows syntax errors with red underlines
2. Check "Select Encoding" in status bar
3. Choose "UTF-8" without BOM
4. Save file

Advanced notes

When receiving package.json from others, always validate before use. CI/CD should validate JSON files as part of linting. Configure your editor to show invisible characters and use UTF-8 without BOM. Windows Notepad used to add BOM - modern versions fixed this. Git can help detect encoding issues with .gitattributes.

Frequently asked questions

What does "npm ERR! Unexpected token in JSON at position" mean?
This error provides specific information about where JSON parsing failed. The "unexpected token" is the character that the parser didn't expect at that position. Common unexpected tokens include: } (extra closing brace), , (trailing comma), ' (single quote), or even invisible characters.
How do I fix "npm ERR! Unexpected token in JSON at position"?
The "Unexpected token" JSON error occurs when npm encounters an invalid character while parsing package.json. This typically indicates a syntax error at the reported position.
What causes "npm ERR! Unexpected token in JSON at position"?
Invisible Unicode characters BOM (Byte Order Mark) at start of file CRLF vs LF line ending issues
Is the "npm ERR! Unexpected token in JSON at position" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading