228

I am assigning a property to the global window object, but when I run eslint, I get this:

"window" is not defined

I see this here in the eslint docs:

the following defines window as a global variable for code that should not trigger the rule being tested:

valid: [
  {
    code: "window.alert()",
    globals: [ "window" ]
  }
]

I've tried adding something like this to the package.json file to have eslint allow "window" as a global variable, but I must be doing something wrong. From the docs it seems like I might need to do something like this in a separate file, but is there a way to define some allowed global variables right in the package.json file?

chevin99
  • 4,946
  • 7
  • 24
  • 32
  • FYI, if you're using node and want ESLint to recognize `global` then you need to ensure `"node": true` is set under your `"env"` configuration. – Joshua Pinter Feb 18 '18 at 17:24
  • I believe it would be nice to have https://stackoverflow.com/a/32481806/9888500 as the default answer as it's the most accurate one? – Esteban Borai Jan 03 '22 at 15:46

6 Answers6

399

There is a builtin environment: browser that includes window.

Example .eslintrc.json:

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

More information: https://eslint.org/docs/user-guide/configuring/language-options#specifying-environments

Also see the package.json answer by chevin99 below.

Laoujin
  • 9,962
  • 7
  • 42
  • 69
116

I found it on this page: http://eslint.org/docs/user-guide/configuring

In package.json, this works:

"eslintConfig": {
  "globals": {
    "window": true
  }
}
simonzack
  • 19,729
  • 13
  • 73
  • 118
chevin99
  • 4,946
  • 7
  • 24
  • 32
  • 65
    the right way to do it is to use "env":{"browser": true} – Nicolas Feb 19 '16 at 09:19
  • 1
    @Nicolas, yep, I probably would've used the method you suggested if that's what I had found first, but this answer is at least useful in showing you can have your eslint config in package.json. – chevin99 Feb 19 '16 at 14:05
  • 4
    It's also possible to inline globals for eslint like this: /*global angular: true */ – Mirko Jun 06 '16 at 15:23
  • 1
    the question specifically asks how to use the package.json file though – virtualLast Aug 18 '17 at 09:58
65

Add .eslintrc in the project root.

{
  "globals": {
    "document": true,
    "foo": true,
    "window": true
  }
}
Neithan Max
  • 11,004
  • 5
  • 40
  • 58
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • Somehow placing eslingConfig in package.json didn't work for me (apart from being conceptually wrong). Adding it in .eslintrc.json works though. – Petrunov Jan 05 '17 at 09:54
  • 1
    @Petrunov `.eslintrc.json` can just be `.eslintrc` – Kirk Strobeck Jan 06 '17 at 00:29
  • this doesn't work in one case i'm experiencing - i am using eslint with gulp for a chrome extension project. in globals i set "chrome": true and it still throws an error about it being an unrecognized global. – Stephen Tetreault Jun 17 '17 at 19:07
45

Your .eslintrc.json should contain the text below.
This way ESLint knows about your global variables.

{
  "env": {
    "browser": true,
    "node": true
  }                                                                      
}
Razzi Abuissa
  • 3,337
  • 2
  • 28
  • 29
Sergey Andreev
  • 1,398
  • 3
  • 16
  • 26
21

I'm aware he's not asking for the inline version. But since this question has almost 100k visits and I fell here looking for that, I'll leave it here for the next fellow coder:

Make sure ESLint is not run with the --no-inline-config flag (if this doesn't sound familiar, you're likely good to go). Then, write this in your code file (for clarity and convention, it's written on top of the file but it'll work anywhere):

/* eslint-env browser */

This tells ESLint that your working environment is a browser, so now it knows what things are available in a browser and adapts accordingly.

There are plenty of environments, and you can declare more than one at the same time, for example, in-line:

/* eslint-env browser, node */

If you are almost always using particular environments, it's best to set it in your ESLint's config file and forget about it.

From their docs:

An environment defines global variables that are predefined. The available environments are:

  • browser - browser global variables.
  • node - Node.js global variables and Node.js scoping.
  • commonjs - CommonJS global variables and CommonJS scoping (use this for browser-only code that uses Browserify/WebPack).
  • shared-node-browser - Globals common to both Node and Browser.

[...]

Besides environments, you can make it ignore anything you want. If it warns you about using console.log() but you don't want to be warned about it, just inline:

/* eslint-disable no-console */

You can see the list of all rules, including recommended rules to have for best coding practices.

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
2

If you are using Angular you can get it off with:

"env": {
    "browser": true,
    "node": true
},
"rules" : {
    "angular/window-service": 0
 }
AndreaM16
  • 3,917
  • 4
  • 35
  • 74