ESLint: 'globalThis' is not defined

824 views Asked by At

There are similar questions with this same error message, but every one I found is a runtime error. For me, globalThis does exist at runtime, but ESLint reports that it does not. How do I tell ESLint that globalThis exists?

Note: I created this to answer it myself. I thought it should be documented outside of an obscure github issue.

2

There are 2 answers

0
Daniel Kaplan On

@mdjermanovic documented the fix here:

globalThis is already supported. Since it's an ES2020 feature, you'll need to enable es2020 (or higher) environment in your configuration.

{
    "env": {
        "es2020": true
    }
} 

Here's online demo with es2020 and no-undef enabled.

Originally posted by @mdjermanovic in https://github.com/eslint/eslint/issues/15199#issuecomment-948724014


Note that you can find older answers that suggest adding globalThis as an eslint "global" config, but the above solution is a more modern approach.

1
Keyboard Corporation On

You can also tell ESLint that globalThis exists.

Specify it in the ESLint configuration file by adding globalThis to the globals section.

{
   "globals": {
       "globalThis": "readonly"
   }
}