Categories
JavaScript Answers

How to Fix the ESLint ‘error no-undef for document’ Error?

Spread the love

Sometimes, we may run into the ESLint ‘error no-undef for document’ error when we’re developing our JavaScript app.

In this article, we’ll look at how to fix the ESLint ‘error no-undef for document’ error when we’re developing our JavaScript app.

Fix the ESLint ‘error no-undef for document’ Error

To fix the ESLint ‘error no-undef for document’ error when we’re developing our JavaScript app, we can add:

"browser": true

into the env section of the ESLint config file.

To do this, we write:

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

And we add "document": false into the globals section with:

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

This way, ESLint will assume that the app runs in the browser environment and so the document object will be available.

And so ESLint won’t emit the error.

We can also do the same thing from the CLI.

To set browser to true , we run:

eslint --env browser,node file.js

And to include document in globals , we run:

eslint --global document file.js

Conclusion

To fix the ESLint ‘error no-undef for document’ error when we’re developing our JavaScript app, we can add:

"browser": true

into the env section of the ESLint config file.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *