解决linter错误

时间:2022-09-19 22:33:18

I am using airbnb extension for linting my React Project. Now, in my index.js I have:

我正在用airbnb的扩展来做我的反应项目。现在,在我的指数。js我有:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <App />,
  document.getElementById('root'),
);

linter says:

剥绒机说:

no-undef 'document' is not defined.at line 8 col 3

How can I solve this problem?

我如何解决这个问题?

1 个解决方案

#1


53  

There are a number of ways to solve/get around this. The two key ways are to either specify document as global or to set the eslint-env as browser (what you probably want). You can do this 1) in-file, 2) in the configuration, or even 3) when running from the CLI.

有很多方法可以解决这个问题。两种关键的方法是将文档指定为全局的,或者将eslin -env设置为浏览器(您可能想要的)。您可以在配置中执行这一操作(1)、2),甚至在从CLI运行时执行3)。

1) In-file:

1)文件内部:

  1. Set the environment as browser in your file:

    将环境设置为您文件中的浏览器:

    /* eslint-env browser */
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    
    ReactDOM.render(
      <App />,
      document.getElementById('root'),
    );
    
  2. Add it as a global in the file itself:

    将其作为全局变量添加到文件中:

    /* global document */
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    
    ReactDOM.render(
      <App />,
      document.getElementById('root'),
    );
    

2) In the eslint configuration:

2)在eslint配置中:

  1. Set the environment as browser in the configuration:

    在配置中设置环境为浏览器:

    {
      "env": {
        "browser": true,
        "node": true
      }
    }
    
  2. Add it as a global in the configuration:

    将其添加为配置中的全局变量:

    {
      "globals": {
        "document": false
      }
    }
    

3) From the CLI:

3)从CLI:

  1. Using env: eslint --env browser,node file.js

    使用env: eslint——env浏览器,node file.js

  2. Using globals: eslint --global document file.js

    使用globals: eslint——global document file.js

Resources:
Specifying Globals with ESLint
Specifying Environments with ESLint
Specifying env with ESLint CLI
Specifying globals with ESLint CLI

参考资料:使用ESLint指定全局变量,使用ESLint指定环境,使用ESLint CLI指定全局变量

#1


53  

There are a number of ways to solve/get around this. The two key ways are to either specify document as global or to set the eslint-env as browser (what you probably want). You can do this 1) in-file, 2) in the configuration, or even 3) when running from the CLI.

有很多方法可以解决这个问题。两种关键的方法是将文档指定为全局的,或者将eslin -env设置为浏览器(您可能想要的)。您可以在配置中执行这一操作(1)、2),甚至在从CLI运行时执行3)。

1) In-file:

1)文件内部:

  1. Set the environment as browser in your file:

    将环境设置为您文件中的浏览器:

    /* eslint-env browser */
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    
    ReactDOM.render(
      <App />,
      document.getElementById('root'),
    );
    
  2. Add it as a global in the file itself:

    将其作为全局变量添加到文件中:

    /* global document */
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App';
    
    ReactDOM.render(
      <App />,
      document.getElementById('root'),
    );
    

2) In the eslint configuration:

2)在eslint配置中:

  1. Set the environment as browser in the configuration:

    在配置中设置环境为浏览器:

    {
      "env": {
        "browser": true,
        "node": true
      }
    }
    
  2. Add it as a global in the configuration:

    将其添加为配置中的全局变量:

    {
      "globals": {
        "document": false
      }
    }
    

3) From the CLI:

3)从CLI:

  1. Using env: eslint --env browser,node file.js

    使用env: eslint——env浏览器,node file.js

  2. Using globals: eslint --global document file.js

    使用globals: eslint——global document file.js

Resources:
Specifying Globals with ESLint
Specifying Environments with ESLint
Specifying env with ESLint CLI
Specifying globals with ESLint CLI

参考资料:使用ESLint指定全局变量,使用ESLint指定环境,使用ESLint CLI指定全局变量