Encountering JSON parsing issue when trying to run npm install on Angular project

npm install issues due to JSON parsing error

I pulled my Angular project from a Git repository and I’m attempting to install the required dependencies. Whenever I run npm install, an error occurs related to JSON parsing. It disrupts the entire installation process.

The error points out something like “Unexpected end of JSON input” and after looking at the npm debug log, this is the stack trace I got:

30601 verbose stack SyntaxError: Unexpected end of JSON input while parsing near '...19"},"dist":{"integri'
30601 verbose stack     at JSON.parse (<anonymous>)
30601 verbose stack     at parseJson (/usr/lib/node_modules/npm/node_modules/json-parse-better-errors/index.js:7:17)
30601 verbose stack     at /usr/lib/node_modules/npm/node_modules/node-fetch-npm/src/body.js:96:50
30602 verbose cwd /home/mrcoder/Projects/Angular/Ava/PFA/pfa-front-cpl
30603 verbose Linux 5.4.0-33-generic
30604 verbose argv "/usr/bin/node" "/usr/bin/npm" "install"
30605 verbose node v12.17.0
30606 verbose npm  v6.14.4
30607 error Unexpected end of JSON input while parsing near '...19"},"dist":{"integri'

Here’s how my package.json file looks:

{
  "name": "vex",
  "version": "9.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "node --max_old_space_size=6144 ./node_modules/@angular/cli/bin/ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.7",
    "@angular/cdk": "~9.1.3",
    "@angular/common": "~9.0.7",
    "@angular/compiler": "~9.0.7",
    "@angular/core": "~9.0.7",
    "@angular/flex-layout": "9.0.0-beta.29",
    "@angular/forms": "~9.0.7",
    "@angular/material": "~9.1.3",
    "@angular/platform-browser": "~9.0.7",
    "@angular/platform-browser-dynamic": "~9.0.7",
    "@angular/router": "~9.0.7",
    "rxjs": "~6.5.4"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "~9.0.0",
    "@angular-devkit/build-angular": "~0.900.7",
    "@angular/cli": "~9.0.7",
    "@angular/compiler-cli": "~9.0.7",
    "@types/node": "~13.9.3",
    "typescript": "~3.7.5"
  }
}

Can anyone help me understand what’s causing this problem and how I can resolve it? Any advice would be greatly appreciated.

Check your disk space first - this error usually happens when npm runs out of room while downloading packages and cuts off the JSON mid-write. I’ve hit this tons of times on dev machines with full drives. Run df -h on Linux or check your available space on Windows. If it’s low, free up some room and try again. Also double-check that your package.json isn’t corrupted by running it through a JSON validator online. Git merge conflicts or encoding problems can add invisible characters that break parsing even when everything looks fine.

this happens when npm gets interrupted mid-download. delete package-lock.json and run npm install again. still broken? check your internet - spotty connections cause partial downloads that break the json parsing.

I got tired of dealing with npm headaches manually, so I automated the whole thing. Those JSON parsing errors? Usually means your package registry calls are getting cut off or corrupted.

Built a workflow that handles this stuff automatically. When someone pulls a project and npm fails, it cycles through different registries, clears caches, and keeps retrying until it works. No more manual cache clearing or guessing if it’s your network.

It validates package.json first and switches between npm, yarn, or pnpm if one craps out. Everything gets logged so you know exactly what broke.

For Angular projects, I added node version checks and automatic ngcc handling since that postinstall script loves to cause problems.

Saves me hours every week instead of troubleshooting this stuff by hand. You can set up similar automation at https://latenode.com

I encountered a similar JSON parsing issue previously, often linked to a corrupted npm cache or incomplete downloads. A helpful approach is to run npm cache clean --force to clear any potential issues with the cache. Subsequently, delete your node_modules directory and the package-lock.json file. After cleaning up, attempt to run npm install again. If you continue to experience problems, consider switching to a different npm registry by using npm config set registry https://registry.npmjs.org/. This can resolve issues related to network errors or problematic cached responses.

Had this exact issue last month with Angular 9. It’s usually npm’s metadata getting corrupted during package resolution. First, update npm with npm install -g npm@latest - version 6.14.4 had registry communication bugs. Then clear everything and reinstall. Still broken? Check if your corporate network or VPN is messing with downloads. I’ve seen proxies strip or cut off npm registry responses, which breaks the JSON.

Run npm install --verbose to see exactly where it’s failing. Sometimes the registry server has issues and returns malformed responses. If that’s happening, wait 30 minutes and try again, or temporarily switch to a mirror with npm install --registry https://registry.npm.taobao.org/