npm install fails with Unexpected end of JSON input while parsing
npm 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.
Fix it fast
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 registry2Match what you see, then jump to its fix
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 verify2Fix
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 install3Verify
The install finishes and creates node_modules.
ls node_modules | headConfirmed by docs.npmjs.com
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 ping2Fix
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 install3Verify
npm ping prints PONG, then the install finishes.
npm pingConfirmed by docs.npmjs.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix