Global variables in Javascript and ESLint

marco_sap picture marco_sap · Jan 9, 2017 · Viewed 55.5k times · Source

I have got multiple javascript files and I have defined some global variable in a file which loads before the others. As a consequence all of the files loaded after the first have access to the global variable. However ESLint shows the global variable as "not defined". I don't want to change the rules of ESLint and I would like to find an elegant way to get rid of these error messages. Any clue? Thanks

Answer

v-andrew picture v-andrew · Jan 9, 2017

I don't think hacking ESLint rules per file is a great idea.

You should rather define globals in .eslintrc or package.json.

For .eslintrc:

"globals": {
    "angular": true
}

For package.json:

"eslintConfig": {
    "globals": {
        "angular": true
    }
}

PS

Reading documentation is a virtue mandatory for developers

https://eslint.org/docs/user-guide/configuring#specifying-globals