npm install fails with FETCH_ERROR: invalid json response body
npm ERR! code FETCH_ERRORShort answer
npm asked a registry for package data and got something that is not JSON, usually an HTML page. That means npm is not talking to the real registry: the registry setting points elsewhere, or a proxy setting sends the request through something that answers with a web page. Check both with npm config.
Fix it fast
1Find out which cause you have
Print the registry and proxy values npm is using, then fetch the registry address yourself.
npm config get registry npm config get proxy npm config get https-proxy2Match what you see, then jump to its fix
- 1
npm config get registry does not print https://registry.npmjs.org/ and you did not choose a private registryThe registry setting points at the wrong addressFix - 2
proxy or https-proxy prints a value, or HTTP_PROXY and HTTPS_PROXY are set in the shellA proxy setting sends the request to a server that does not return registry dataFix
- 1
What does the error look like?
npm ERR! code FETCH_ERRORnpm ERR! errno FETCH_ERRORnpm ERR! invalid json response body
Cause 1 of 2
The registry setting points at the wrong address
1Check
npm reads registry from the project .npmrc, your user .npmrc, the global file and the environment. A dead mirror or a leftover address in any of them wins over the default.
npm config get registry
cat .npmrc2Fix
Remove the setting, or set the registry back to the default.
npm config delete registry
# or
npm config set registry https://registry.npmjs.org/3Verify
Retry the install.
npm installConfirmed by docs.npmjs.com
Cause 2 of 2
A proxy setting sends the request to a server that does not return registry data
1Check
npm uses the proxy and https-proxy settings, and also reads the HTTP_PROXY and HTTPS_PROXY environment variables. An old or wrong proxy can answer with something that is not registry data.
npm config get proxy
npm config get https-proxy
env | grep -i proxy2Fix
Remove stale proxy settings, or correct them to your proxy's address. Use noproxy for hosts that must bypass it.
npm config delete proxy
npm config delete https-proxy3Verify
Retry the install.
npm installConfirmed by docs.npmjs.com
How do I stop it coming back?
- Keep registry settings in one project .npmrc that is committed, not scattered across machines.
- After leaving a company VPN or proxy network, unset its npm proxy settings.
- Print npm config get registry in CI logs so a wrong address is visible.
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