error 'document' is not defined : eslint / React

前端 未结 4 1121
青春惊慌失措
青春惊慌失措 2021-02-04 23:47

I\'m building a React app, with create-react-app.

I got the following error when running ESLint:
8:3 error \'document\' is not defined no-undef.

相关标签:
4条回答
  • 2021-02-05 00:13

    In your package.json, specify the test environment for jest as "jsdom" as follows:

      "jest": {
        "testEnvironment": "jsdom"
      }
    

    I faced the same issue and it worked well for me.

    0 讨论(0)
  • 2021-02-05 00:16

    Add "browser": true your env to include global variables (like document, window, etc.)

    See the ESLint environment documentation for more information.

    0 讨论(0)
  • 2021-02-05 00:22

    If you have .eslintrc.json file then add following code snippet.

    {
        ...otherSettings,
        "env": {
            "browser": true,
            "node": true
        }
    }
    

    if you do not have .eslintrc.json then you can add these settings in you package.json file.

    {
        ...otherSettings,
        "eslintConfig": {
          "globals": {
            "window": true
          }
        }
    }
    
    0 讨论(0)
  • 2021-02-05 00:32

    set an env property in you eslint.rc file, e.g.

    {
        "env": {
            "browser": true,
            "node": true
        }
    }
    
    0 讨论(0)
提交回复
热议问题