Uncaught ReferenceError: Cannot access 'XMLHttpRequest' before initialization
See original GitHub issueYou 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:
- Created 3 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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…
This should be fixed by https://github.com/socketio/engine.io-client/commit/c22681542cc4b0acf2dc5ac2923fa3faa33b6008 (included in
engine.io-client@4.0.5
).