Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalThis is not defined #10910

Closed
icecream17 opened this issue May 2, 2021 · 5 comments
Closed

globalThis is not defined #10910

icecream17 opened this issue May 2, 2021 · 5 comments

Comments

@icecream17
Copy link

"duplicate?": Probably because of #10636

Describe the bug

When I use globalThis I get the following error:

image

Did you try recovering your dependencies?

npm --version
7.10.0

Which terms did you search for in User Guide?

I searched globalThis. While typing it was on global and the autocomplete suggestions included this: https://create-react-app.dev/docs/using-global-variables/

which provides a workaround: Use window instead. However i'd rather use globalThis anyways.

Environment

(paste the output of the command here.)

PS C:\Users\Steven\Documents\code\js\sudoku\solver> create-react-app --info    
 
Environment Info:

  current version of create-react-app: 4.0.3
  running from C:\Users\Steven\AppData\Roaming\npm\node_modules\create-react-app

  System:
    OS: Windows 10 10.0.19042
    CPU: (4) x64 Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz  
  Binaries:
    Node: 16.0.0 - C:\Program Files\nodejs\node.EXE       
    Yarn: Not Found
    npm: 7.11.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 90.0.4430.93
    Edge: Spartan (44.19041.906.0), Chromium (90.0.818.51)
    Internet Explorer: Not Found
  npmPackages:
    react: ^17.0.2 => 17.0.2
    react-dom: ^17.0.2 => 17.0.2
    react-scripts: 4.0.3 => 4.0.3
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

(Write your steps here:)

  1. use globalThis

Expected behavior

(Write what you thought would happen.)

It would work

Actual behavior

(Write what happened. Please add screenshots!)

### Describe the bug

When I use `globalThis` I get the following error:

image

Reproducible demo

untested demo

// index.js
console.log(globalThis)

// <default code>

(Paste the link to an example project and exact instructions to reproduce the issue.)

@xyy94813
Copy link

xyy94813 commented Aug 5, 2021

Is there any workaround before release?

@icecream17
Copy link
Author

I provided a workaround: Use window instead of globalThis, since in browsers window === globalThis
It's not great but it works.

@turboezh
Copy link

Hi. What about TypeScript? I try const theValue = window.myAwesomeGlobalVar; but it still claims that TS2339: Property 'myAwesomeGlobalVar' does not exist on type 'Window & typeof globalThis'.

@icecream17
Copy link
Author

icecream17 commented Aug 31, 2021

declare global {
   interface Window {
      myAwesomeGlobalVar: YourType
   }
}

The best solution is to import your variables if you can...

@icecream17
Copy link
Author

icecream17 commented Oct 19, 2021

fixed with general dependency updates in create-react-app v5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants