How to fix npm ERR! code EJSONPARSE npm ERR! Duplicate key in npm

npmBEGINNERMEDIUMUpdated Dec 6, 2025· 192 views

The EJSONPARSE error with duplicate key occurs when your package.json contains multiple properties with the same name. This typically happens after git merge conflicts or manual editing errors.

Quick fix

Fixes npm ERR! code EJSONPARSE npm ERR! Duplicate key

# Search for potential duplicates
grep -n '"dependencies"' package.json
grep -n '"devDependencies"' package.json
grep -n '"scripts"' package.json
grep -n '"name"' package.json
  1. Validate JSON syntax online
  2. Search for duplicate top-level properties
  3. Remove git merge conflict markers
  4. Fix comma issues
npm — error
# Search for potential duplicates
grep -n '"dependencies"' package.json
grep -n '"devDependencies"' package.json
grep -n '"scripts"' package.json
grep -n '"name"' package.json
npm ERR! code EJSONPARSE
npm ERR! Duplicate key

What this error means

The EJSONPARSE error indicates npm's JSON parser encountered invalid syntax in your package.json file. Unlike JavaScript engines that silently accept duplicate keys (keeping only the last value), npm uses a strict JSON parser that explicitly rejects files containing multiple keys with the same name at the same nesting level. This error commonly surfaces when package.json has been corrupted through incomplete merge conflict resolution, where markers like `<<<<<<<`, `=======`, and `>>>>>>>` remain in the file, or when multiple sections like `dependencies` appear twice due to a faulty merge. The parser halts immediately upon finding duplicate keys, preventing any npm command from executing.

Typical symptoms

  • npm install fails immediately with EJSONPARSE error
  • npm start, npm run, and other commands refuse to execute
  • Error message shows line number where parsing failed
  • CI/CD pipelines fail during dependency installation
  • Recent git merge or manual edit preceded the error
  • IDE or text editor may not highlight the problem

Common causes

  1. Duplicate property names at the same JSON level (two dependencies sections)
  2. Unresolved git merge conflict markers in package.json
  3. Missing commas between key-value pairs
  4. Trailing comma after the last property in an object
  5. Duplicate package entries within dependencies or devDependencies
  6. Corrupted or truncated package.json file
  7. Copy-paste error introducing duplicate sections

How to fix "npm ERR! code EJSONPARSE npm ERR! Duplicate key"

1Validate JSON syntax online

Copy your package.json contents into a JSON validator like JSONLint to identify the exact location of duplicate keys or syntax errors:

1. Go to https://jsonlint.com/
2. Paste your entire package.json
3. Click "Validate JSON"
4. Look for "Duplicate key" warnings in the results

2Search for duplicate top-level properties

Open package.json and search for common duplicated sections. Each of these should appear only once:

bash
# Search for potential duplicates
grep -n '"dependencies"' package.json
grep -n '"devDependencies"' package.json
grep -n '"scripts"' package.json
grep -n '"name"' package.json

If any search returns multiple line numbers, you have duplicates to merge.

3Remove git merge conflict markers

Check for and remove any leftover merge conflict markers:

bash
# Search for conflict markers
grep -n "<<<<<<<\|=======\|>>>>>>>" package.json

If found, edit package.json to:
1. Remove all <<<<<<< lines
2. Remove all ======= lines
3. Remove all >>>>>>> lines
4. Keep only the correct version of conflicting code

4Fix comma issues

Check for missing or trailing commas:

Missing comma (invalid):

json
{
  "name": "my-app"
  "version": "1.0.0"
}

Trailing comma (invalid in JSON):

json
{
  "name": "my-app",
  "version": "1.0.0",
}

Correct:

json
{
  "name": "my-app",
  "version": "1.0.0"
}
5Merge duplicate sections

If you have two dependency sections, combine them into one:

Before (invalid):

json
{
  "dependencies": {
    "express": "^4.18.0"
  },
  "dependencies": {
    "lodash": "^4.17.0"
  }
}

After (valid):

json
{
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.0"
  }
}
6Clear npm cache and reinstall

After fixing the JSON, clear npm cache and reinstall:

bash
# Clear npm cache
npm cache clean --force

# Remove existing artifacts
rm -rf node_modules package-lock.json

# Reinstall dependencies
npm install
7Regenerate package.json if severely corrupted

If the file is too corrupted to fix manually, regenerate it:

bash
# Backup current file
mv package.json package.json.bak

# Create fresh package.json
npm init -y

# Manually copy dependencies from backup
# Then reinstall
npm install

Advanced notes

For CI/CD pipelines, add a pre-merge validation step that checks package.json syntax before allowing merges. Tools like find-duplicated-property-keys can programmatically detect duplicate keys. Modern IDEs like VS Code and WebStorm highlight duplicate JSON keys in real-time—ensure JSON validation is enabled in your editor settings.

In Docker builds, this error halts the entire build process. Validate package.json on the host machine before the COPY command in your Dockerfile to catch errors earlier in the development cycle.

Frequently asked questions

What does "npm ERR! code EJSONPARSE npm ERR! Duplicate key" mean?
json file. Unlike JavaScript engines that silently accept duplicate keys (keeping only the last value), npm uses a strict JSON parser that explicitly rejects files containing multiple keys with the same name at the same nesting level. json has been corrupted through incomplete merge conflict resolution, where markers like <<<<<<<, =======, and >>>>>>> remain in the file, or when multiple sections like dependencies appear twice due to a faulty merge.
How do I fix "npm ERR! code EJSONPARSE npm ERR! Duplicate key"?
The EJSONPARSE error with duplicate key occurs when your package.json contains multiple properties with the same name. This typically happens after git merge conflicts or manual editing errors.
What causes "npm ERR! code EJSONPARSE npm ERR! Duplicate key"?
Duplicate property names at the same JSON level (two dependencies sections) Unresolved git merge conflict markers in package.json Missing commas between key-value pairs
Is the "npm ERR! code EJSONPARSE npm ERR! Duplicate key" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading