I've been a long time user of Standard, and now that I'm working on a new project, I've been asked to start writing semicolons.
I'm trying to use both xo, Babel and React, but I keep getting an error when I try to lint my code:
document is not defined. no-undef
I've tried adding an env
option to the xo
field in my package.json file, but no success.
My xo
config:
"xo": {
"esnext": true,
"extends": "xo-react",
"space": true,
"rules": {
"react/jsx-space-before-closing": 0
}
}
To solve the"ReferenceError: document is not defined" error, make sure to only use the document global variable on the browser. The variable relates to the Document Object Model, which represents a web page that is loaded in the browser and can't be used on the server side (e.g. in Node. js).
The most common reason for getting the reference error while on the browser is when you try to access the document object too early. The best way to resolve this is to just move your code to the bottom of the page so that the document will be ready by the time your code references it.
A not defined error is when we did not declare the variable and tried to call that variable. In JavaScript, we can declare variables without adding const , let , or var , and we won't get an error of undefined or not defined . This can be seen in the code below.
The "ReferenceError: path is not defined" occurs when we use the path module without importing it in a Node. js application. To solve the error, make sure to import the path module before using it - import path from 'path' . To solve the error, import the path module before using it.
It is cumbersome to specify linting options such as /** global document **/
and edit a configuration file every time you use a global.
This error can be suppressed by using --env=browser
option:
xo --env=browser [<file|glob> ...]
Note: Same problem comes with Node.js, where the linter will complain that require
and friends are not defined. The switch has to change to --env=node
in that case.
However, XO defaults the env
to node
, therefore this will not be a problem in most cases. You will need multiple environments if your project contains both client and server files. in that case, --env
switch can be set multiple times:
xo --env=browser --env=node [<file|glob> ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With