React and Electron with Draft.js: "Global is not defined"
Asked Answered
C

1

8

There is a similar question raised here: Uncaught ReferenceError: global is not defined at Object../node_modules/fbjs/lib/setImmediate.js

And there are two answers on that question that both seem confident they can solve it. The problem for me is--no explanation of how to implement the answers is given.

Basically, I have an electron app incorporating React, everything works fine until I try to implement draft.js into the project. I get the following error:

app.js:19805 Uncaught ReferenceError: global is not defined

The log points me to a line in the babel/webpack-compiled app.js:

module.exports = global.setImmediate;

The specific thing that causes it is narrowed down to an import statement:

import {Editor, EditorState} from 'draft-js';

One of the answers in the linked question above says to add a global object on the window:

(window as any).global = window;

What I can't figure out is... what does that mean? In my main.js file, where I create the window (it's just the one window for the entire app) trying to do this throws an error seemingly no matter where I try to do it, and I can't seem to find any reference to defining 'global' at all. Where am I meant to define 'global'?

Calends answered 5/1, 2021 at 21:35 Comment(1)
Check my answer in the link that you provided in your question. Basically, add this in you index.html file (inside the markups): <script> const global = globalThis; </script>Overcurious
D
31

global got renamed to globalThis in the tc39 proposal https://github.com/facebook/fbjs/issues/290

just add this to index.html that's how i got it fixed

<script>
      const global = globalThis;
</script>
Dantzler answered 30/4, 2021 at 15:20 Comment(1)
Wow! This worked perfectly. Hope global is not used for something else(?). Just wondering if this could have side effects.Prosit

© 2022 - 2024 — McMap. All rights reserved.