question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: XMLHttpRequest is not defined on SSR

See original GitHub issue
ReferenceError: XMLHttpRequest is not defined
    at Object.ajax (/code/frontend/node_modules/i18next-xhr-backend/dist/commonjs/ajax.js:46:5)
    at /code/frontend/node_modules/i18next-xhr-backend/dist/commonjs/index.js:110:24
    at Array.forEach (<anonymous>)
    at Backend.create (/code/frontend/node_modules/i18next-xhr-backend/dist/commonjs/index.js:107:17)
    at /code/frontend/node_modules/i18next-chained-backend/dist/commonjs/index.js:92:25
    at Array.forEach (<anonymous>)
    at Backend.create (/code/frontend/node_modules/i18next-chained-backend/dist/commonjs/index.js:91:21)
    at Connector.saveMissing (/code/frontend/node_modules/i18next/dist/commonjs/BackendConnector.js:295:20)
    at send (/code/frontend/node_modules/i18next/dist/commonjs/Translator.js:195:37)
    at Translator.translate (/code/frontend/node_modules/i18next/dist/commonjs/Translator.js:210:13)
    at I18n.t (/code/frontend/node_modules/i18next/dist/commonjs/i18next.js:356:73)
    at Interpolate.fixedT [as t] (/code/frontend/node_modules/i18next/dist/commonjs/i18next.js:342:21)
    at Interpolate.render (/code/frontend/node_modules/react-i18next/dist/commonjs/Interpolate.js:58:25)
    at d (/code/frontend/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:28:211)
    at wa (/code/frontend/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:28:493)
    at a.render (/code/frontend/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:33:18)
i18n
    .use(Backend)
    .use(intervalPlural)
    .use(languageDetector)
    .use(reactI18nextModule)
    .init(
        {
            fallbackLng: false,
            saveMissing: true,
            appendNamespaceToCIMode: true,

            // allow keys to be phrases having `:`, `.`
            nsSeparator: false,
            keySeparator: false,

            // have a common namespace used around the full app
            ns: ['translations'],
            defaultNS: 'translations',

            debug: false,
            react: { wait: true },

            // Language detection
            detection: {
                // order and from where user language should be detected
                order: ['urlDetector', 'querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],

                // keys or params to lookup language from
                lookupCookie: 'locale',
                lookupQuerystring: 'lng',
                lookupLocalStorage: 'i18nextLng',

                // cache user language on
                caches: ['localStorage', 'cookie'],
                excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)

                // optional expire and domain for set cookie
                // cookieMinutes: 10,
                // cookieDomain: 'myDomain',

                // optional htmlTag with lang attribute, the default is:
                // htmlTag: document.documentElement,
            },

            backend: {
                backends: [
                    XHR
                ],

                backendOptions: [
                    {
                        // path where resources get loaded from, or a function
                        loadPath: '/api/translations/{{lng}}/{{ns}}',

                        // path to post missing resources
                        addPath: '/api/translations/add/{{lng}}/{{ns}}',

                        // your backend server supports multiloading
                        // /locales/resources.json?lng=de+en&ns=ns1+ns2
                        allowMultiLoading: false, // set loadPath: '/locales/resources.json?lng={{lng}}&ns={{ns}}' to adapt to multiLoading

                        // allow cross domain requests
                        crossDomain: false,

                        // allow credentials on cross domain requests
                        withCredentials: false,
                    },
                ],
            },
        },
        error => {
            if (error) {
                logger.error(error);
            }
        }
    );

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

43reactions
arsnlcommented, Aug 28, 2018

xhr-backend is not "universal"

wat

8reactions
arsnlcommented, Aug 28, 2018

@tavurth

All you have to do is to define another “ajax” (https://github.com/i18next/i18next-xhr-backend/blob/0ec7d848236c379a90017fff53640380a4d0d46c/src/index.js#L11)

I suggest axios.

Eg

import i18next from 'i18next';
import XHR from 'i18next-xhr-backend';
import axios from 'axios';

i18next
  .use(XHR)
  .init({
    backend: {
        ajax: axios
        ...
    }
  });

Now you have a universal backend plugin.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"ReferenceError: XMLHttpRequest is not defined" when using ...
The XMLHttpRequest API isn't provided in node.js. I suppose it's possible to use a module that does but it would probably be better...
Read more >
Node.js - ReferenceError: XMLHttpRequest is not defined
Attempting to run the following JavaScript code (an AJAX call using XMLHttpRequest) throws a ReferenceError under Node, but works in a web browser....
Read more >
jest xmlhttprequest is not defined
Node.js - Errors - ReferenceError: XMLHttpRequest is not defined Attempting to run the following JavaScript code (an AJAX call using XMLHttpRequest) throws ...
Read more >
xmlhttprequest is not defined nextjs
Next js executes this code on the server side and that's why the error is thrown. The way the entry point is resolved...
Read more >
How to dynamically modify a JS library import to remove next ...
I believe that the XMLHttpRequest is not defined error comes from the attempted pre-rendering of rescript-request requests ( XMLHttpRequest is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found