Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm install fails with Unexpected end of JSON input while parsing

Errornpm ERR! Unexpected end of JSON input while parsing near '...nt-webpack-plugin":"0'

Short answer

npm read a package document that was cut off or empty. The two checkable causes are a damaged entry in the npm cache and a registry setting that points somewhere that does not answer properly. Run npm cache verify first, clean the cache with --force if it still fails, then ping the registry.

  • Applies to npm 10, Node.js 22, Angular CLI 20
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out where npm keeps its cache and which registry it talks to. Both appear in this output.

    npm config get cache
    npm config get registry
  2. 2Match what you see, then jump to its fix

    1. 1The same package name appears in the error on every runA damaged entry in the npm cacheFix
    2. 2The error changes package from run to run, or only happens on one network or machineThe registry does not answer correctlyFix

What does the error look like?

  • npm ERR! Unexpected end of JSON input while parsing near '...nt-webpack-plugin":"0'
  • Unexpected end of JSON input while parsing

Cause 1 of 2

A damaged entry in the npm cache

1Check

npm detects bad cache data when it is stored or read and normally repairs itself, but a half-written entry can slip through. Verify the cache folder (the _cacache directory inside the cache location).

npm cache verify

2Fix

If verify does not clear it, delete the cache. npm requires --force for this and calls it usually unnecessary, so treat it as a last step.

npm cache clean --force
npm install

3Verify

The install finishes and creates node_modules.

ls node_modules | head

Confirmed by docs.npmjs.com

Did this fix it?

Cause 2 of 2

The registry does not answer correctly

1Check

Ping the configured registry. The command prints PONG with a time when the registry replies, and an error when it does not.

npm ping

2Fix

Point npm at the registry you mean to use (the default is https://registry.npmjs.org/) and try again.

npm config set registry https://registry.npmjs.org/
npm install

3Verify

npm ping prints PONG, then the install finishes.

npm ping

Confirmed by docs.npmjs.com

Did this fix it?

How do I stop it coming back?

  • Keep one registry setting per project in a project-level .npmrc, so a stray user setting cannot change it.
  • Commit the lockfile and use npm ci in builds.
  • Do not interrupt npm install; a killed install is how a cache entry ends up half written.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.