ReferenceError: XMLHttpRequest is not defined - on production build
January 16, 2019 at 4:16pmReferenceError: XMLHttpRequest is not defined - on production build
January 16, 2019 at 4:16pmHello,
What is the way to fix a 'ReferenceError: XMLHttpRequest is not defined' error? The error occurs every time when I am building the production build. I am trying to build a Gatsby v2 environnement with i18next for my translations.
When I start the development envoironnement, everything works just fine, the translations are rendered. Somehow I can't figure out how to fix it for the production build.
gatsby-ssr.js
i18n.js
gatsby-node.js
January 17, 2019 at 2:20am
January 17, 2019 at 8:27am
Sounds like
XMLHttpRequest
is not properly installed in production's node_modules
. Have you checked it?Yes it's installed in the node_modules, and I can console log the XMLHttpRequest function. I think when I build, ssr can't find it, I see this error for example;
April 23, 2019 at 4:09pm
August 30, 2019 at 2:23am
August 30, 2019 at 9:21am
If it's console-logging fine when loaded in the browser, that doesn't prove that it will work for SSR. XHR is available in the browser by default, but you'll need to polyfill it in Node. If you have the
xhr2
node module installed, you could do something like this in your gatsby-ssr.js
:if (!("XMLHttpRequest" in global)) {global.XMLHttpRequest = require('xhr2');}
you mean need gatsby-ssr.js to set global XMLHttpRequest?
December 19, 2019 at 7:57am
Hi Guys
I'm also facing exactly same error
ReferenceError: XMLHttpRequest is not defined - on production build
Yes sounds like XMLHttpRequest is not global in Gatsby.
Any idea how to over come this error ?September 16, 2020 at 6:40am