DEV Community

Emmanuel Gautier
Emmanuel Gautier

Posted on • Originally published at emmanuelgautier.com on

ESLint Global Variables

Sometimes, we need to access some globally defined variables, especially the browser side when we use third-party libraries, and can also be the case server side. Because the variables are not referenced in the code, eslint throws an error saying the variable is not defined.

Eslint let us specify the global variable. This configuration allows eslint to know that a variable exists even if it is not referenced in the code. Here is an example:

{
  "globals": {
    "dataLayer": true
  }
}
Enter fullscreen mode Exit fullscreen mode

In addition to specifying it, you can declare the variable as writable or not. Here is an example:

{
  "globals": {
    "dataLayer": "writable"
  }
}
Enter fullscreen mode Exit fullscreen mode

Or only declare it as read-only:

{
  "globals": {
    "dataLayer": "readonly"
  }
}
Enter fullscreen mode Exit fullscreen mode

You can find more documentation on the Eslint doc page.

Top comments (0)