ESLint Global Variables

Emmanuel Gautier / November 27, 2021

1 min read

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
  }
}

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

{
  "globals": {
    "dataLayer": "writable"
  }
}

Or only declare it as read-only:

{
  "globals": {
    "dataLayer": "readonly"
  }
}

You can find more documentation on the Eslint doc page.

Consulting

If you're seeking solutions to a problem or need expert advice, I'm here to help! Don't hesitate to book a call with me for a consulting session. Let's discuss your situation and find the best solution together.

Share this post
Follow the RSS feed

Subscribe to the newsletter

Get the latest news about tech new articles and projects.