Maaz Zindani

Loading the index…

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

npm install fails with FETCH_ERROR: invalid json response body

Errornpm ERR! code FETCH_ERROR

Short 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.

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

Fix it fast

  1. 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-proxy
  2. 2Match what you see, then jump to its fix

    1. 1npm 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. 2proxy 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

What does the error look like?

  • npm ERR! code FETCH_ERROR
  • npm ERR! errno FETCH_ERROR
  • npm 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 .npmrc

2Fix

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 install

Confirmed by docs.npmjs.com

Did this fix it?

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 proxy

2Fix

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-proxy

3Verify

Retry the install.

npm install

Confirmed by docs.npmjs.com

Did this fix it?

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 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.