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.

Uncaught ReferenceError: Cannot access 'XMLHttpRequest' before initialization

See original GitHub issue

You want to:

  • report a bug
  • request a feature

Current behaviour

When importing the client in a React or Svelte project I’m getting the Uncaught ReferenceError: Cannot access 'XMLHttpRequest' before initialization error.

Issue is coming from engine.io-client:

const hasXHR2 = (function() {
  const XMLHttpRequest = require("xmlhttprequest-ssl"); //<---- Devtools says the problem is here
  const xhr = new XMLHttpRequest({ xdomain: false });
  return null != xhr.responseType;
})();

Steps to reproduce (if the current behaviour is a bug)

I can’t provide a REPL because I keep getting: Could not find module in path: 'xmlhttprequest-ssl' relative to '/node_modules/engine.io-client/lib/transports/index.js' which somehow seems to be related to the issue described in Current behaviour.

The Uncaught ReferenceError: Cannot access 'XMLHttpRequest' before initialization will show up by simply importing the package like so: import io from 'socket.io-client.

It doesn’t matter what code you have after that line, it’ll crash with the error.

Expected behaviour

I’m expecting to be able to import the package.

Setup

  • OS: macOS & Windows give the issue
  • browser: Edge, Chrome, Firefox and Safari give the issue.
  • socket.io version: Client is 3.0.3 - Server is N/A

Other information (e.g. stacktraces, related issues, suggestions how to fix)

polling-xhr.js:18 Uncaught ReferenceError: Cannot access 'XMLHttpRequest' before initialization
    at polling-xhr.js:18
    at polling-xhr.js:21
    at main.ts:5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
takoyarocommented, Dec 4, 2020

socket.io-client/dist/socket.io.min.js and socket.io-client/dist/socket.io.msgpack.min.js works I guess 🤔

It does “work” in the sense that the error disappears but it does not work as Client<=>Server communication never establishes. Simply importing Socket.io v2 “fixes” my issue…

0reactions
darrachequesnecommented, Dec 7, 2020

This should be fixed by https://github.com/socketio/engine.io-client/commit/c22681542cc4b0acf2dc5ac2923fa3faa33b6008 (included in engine.io-client@4.0.5).

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using `let` or `const` is accessed before it was initialized in the...
Read more >
Cannot access 'variable_name' before initialization
When you assign variables using $: you cannot assign them as part of other variables declared using let , const , or var...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >
Cannot access x before initialization" - Ep 10 - YouTube
JS Casts 10 - How to fix " Uncaught ReferenceError : Cannot access x before initialization " in JavaScript.
Read more >
Uncaught ReferenceError: Cannot access ' ...
Uncaught ReferenceError : Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization ; function getJwt() · if ; in localStorage ; return localStorage ; return ...
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