javascriptjsdom

ESLint: 'globalThis' is not defined


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.


Solution

  • @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.