Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error 'document' is not defined : eslint / React

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.

My app runs without error, but I got this ESLint error in 2 files. See one of those .jsx files and my ESLint configuration.

index.jsx:

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

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

eslintrc.js:

module.exports = {
  "extends": "airbnb",
  "plugins": [
      "react",
      "jsx-a11y",
      "import"
  ],
  "env": {
    "jest": true
  }
};

How can I fix this ? For the moment I will simply ignore both files ... but I'd rather find a long term solution.

like image 654
Thomas Sauvajon Avatar asked Feb 21 '17 20:02

Thomas Sauvajon


4 Answers

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

See the ESLint environment documentation for more information.

like image 94
Lucas Avatar answered Nov 09 '22 04:11

Lucas


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

{
    "env": {
        "browser": true,
        "node": true
    }
}
like image 42
Hom Bahrani Avatar answered Nov 09 '22 02:11

Hom Bahrani


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.

like image 7
Peter Avatar answered Nov 09 '22 04:11

Peter


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
      }
    }
}
like image 5
Ajay Rawat Avatar answered Nov 09 '22 04:11

Ajay Rawat