skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    @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 more modern.


  2. 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"
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search