742

Is it possible to turn off the eslint rule for the whole file? Something such as:

// eslint-disable-file no-use-before-define 

(Analogous to eslint-disable-line.) It happens to me quite often, that in a certain file, I'm breaking a specific rule on many places which is considered OK for that file, but I don't want to disable the rule for the whole project nor do I want to disable other rules for that specific file.

doğukan
  • 23,073
  • 13
  • 57
  • 69
Tomas Kulich
  • 14,388
  • 4
  • 30
  • 35
  • 2
    You can add a rule as the first line of the file as so: /*eslint no-use-before-define: 2*/ – joaumg Jan 13 '16 at 10:33
  • 2
    perfect, that works, thank you. I just overlooked the section in the docs: http://eslint.org/docs/user-guide/configuring.html#configuring-rules – Tomas Kulich Jan 13 '16 at 10:42
  • So many duplicated answers in here.. If a mod could delete all the `/* eslint-disable */` answers except the first one that would be nice – Tofandel May 14 '21 at 13:23

19 Answers19

595

You can turn off/change a particular rule for a file by putting the configurations at the top of the file.

/* eslint no-use-before-define: 0 */  // --> OFF

or

/* eslint no-use-before-define: 2 */  // --> ON

More info

A1rPun
  • 16,287
  • 7
  • 57
  • 90
Gyandeep
  • 12,726
  • 4
  • 31
  • 42
  • 26
    Is there a way to put a rule in `.eslintrc` that enforces all **file specific** rules like this to the top of the file? – Jeremy Oct 19 '16 at 10:25
  • 14
    @Jeremy see my answer. You just need to add an `.eslintignore` file to your project root directory. – prograhammer Dec 12 '16 at 21:58
  • Nice! Saw some JSON-y answers floating around that didn't seem to work for disabling no-camelcase errors, but this (`/* eslint camelcase: 0 */`) did the trick (using eslint via standard.js) – Oli Beatson May 21 '18 at 11:34
  • 8
    I tried /* eslint-disable rule-name */ and it worked – Marcos Pereira Jan 23 '19 at 22:21
  • :1 makes it a Warning instead of Error – Arno Teigseth Feb 09 '19 at 18:33
  • 1
    The double comment formatting in this answer mislead me. – A1rPun Nov 18 '19 at 10:00
  • You can also set env values in a file: `/*eslint-env node */` – Matt Sep 30 '20 at 19:05
  • 2
    @Jeremy Yes. I designed a way to remove specific rules from specific files or folders. Please check my answer right here: https://stackoverflow.com/a/65069069/7644846 @prograhammer Putting the file on `.eslintignore` will disable ALL rules from the files. But @Jeremy want to disable specific rules from files. – Victor Nov 30 '20 at 17:40
  • I upvoted this answer for answering correctly. However, if we want to disable a specific rule from a lot of different files, we need to add the `/* eslint no-use-before-define: 0 */` on *173* different files for example. You can just change in just one file: `.eslintrc` using the `overrides`. I explained more about this on my answer: https://stackoverflow.com/a/65069069/7644846 – Victor Nov 30 '20 at 17:43
  • Using `off` now also works: `/* eslint no-use-before-define: off */` – Nero Vanbiervliet Apr 27 '21 at 12:39
  • `/* eslint no-use-before-define: 2 */` can make the rule more strict. For example from 'warn' to 'error' level It's better to use `/* eslint-disable no-use-before-define */` and later `/* eslint-enable no-use-before-define */` – Anatoliy Litinskiy Aug 11 '21 at 16:15
  • @Jeremy keep scrolling to find my answer at https://stackoverflow.com/a/56719951 for disabling specific file/rules. – Aidin Dec 12 '21 at 05:28
512

To disable a specific rule for the file:

/* eslint-disable no-use-before-define */

Note there is a bug in eslint where single line comment will not work -

// eslint-disable max-classes-per-file
// This fails!

Check this post

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Wahome
  • 5,477
  • 1
  • 11
  • 7
  • 32
    This will disable all eslint rules within that block, not just the one `no-use-before-define` intended to ignore – Jeremy Oct 19 '16 at 10:22
  • 16
    Basically that is the whole idea. The question is "Is it possible to turn off the eslint rule for the **whole file**? " – Wahome Jan 20 '17 at 09:21
  • 31
    @Wahome "Is it possible to turn off the eslint **rule** for the whole file?" The word rule is *singular*. Your answer turns off all *rules* plural. – spencer.sm Feb 06 '18 at 22:46
  • 8
    in the description he mentioned that he wants soemthing like disabling specific rule- read again the question! – Marwen Trabelsi Mar 14 '18 at 09:07
  • 5
    It's true, this doesn't address the whole question, but still a useful answer since this thread is a top result when googling "eslint ignore file" – Robin Métral Oct 26 '19 at 11:27
  • 4
    Just append the rule(s) at the end `/* eslint-disable no-use-before-define */` – Sébastien Loix Feb 27 '20 at 04:59
  • 2
    The `/*` and `*/`, apparently, matter (`//` didn't work). – acdcjunior May 21 '20 at 13:21
  • See my answer (https://stackoverflow.com/a/56719951) if you want to have more granular control on which files/lines/rules to ignore. – Aidin Aug 27 '21 at 19:35
  • 5
    For anyone else confused by the comments, `/* eslint-disable no-use-before-define */` is definitely a correct answer which disables a *specific* rule for the whole file. Notice the edits in the answer. The comments were directed at the original answer which was "Just place /* eslint-disable */ at the top of the file." – Asa Sep 14 '22 at 01:03
368

Let's have a breakdown of the scenarios, like you always do, dear awesome developer!

Here are two questions to ask yourself, first.

Question One: How many "rules" do you want to ignore?

  1. All Rules
  2. One or more Specific Rules (e.g. quotes or semi)

Question Two: How many "lines/files" do you want to ignore?

  1. One or more Lines
  2. All lines in one or more Files
  3. Everywhere

Now, based on the your answers, there are 2 × 3 = 6 cases.


1) Disabling "All rules"


Case 1.1: You want to disable "All Rules" for "One or more Lines"

Two ways you can do this:

  1. Put /* eslint-disable-line */ at the end of the line(s),
  2. or /* eslint-disable-next-line */ right before the line.

Case 1.2: You want to disable "All Rules" for "One File"

  • Put the comment of /* eslint-disable */ at the top of the file.

Case 1.3: You want to disable "All rules" for "Some Files"

There are 3 ways you can do this:

  1. You can go with 1.2 and add /* eslint-disable */ on top of the files, one by one.
  2. You can put the file name(s) in .eslintignore. This works well, especially if you have a path that you want to be ignored. (e.g. apidoc/**)
  3. Alternatively, if you don't want to have a separate .eslintignore file, you can add "eslintIgnore": ["file1.js", "file2.js"] in package.json as instructed here.

2) Disabling "Some Rules"


Case 2.1: You want to disable "Some Rules" for "One or more Lines"

Two ways you can do this:

  1. You can put /* eslint-disable-line quotes */ (replace quotes with your rules) at the end of the line(s),

  2. or /* eslint-disable-next-line no-alert, quotes, semi */ before the line.

Pro Tip: if you have multiple lines that you want to ignore the same rule(s) for, you can disable and enable the rules like this:

// Assume these lines are long engouh that max-len is gonna trigger

/* eslint-disable max-len */
console.log("I am a loooooooooo...ooooong line 1, but eslint doesn't bug!");
console.log("I am a loooooooooo...ooooong line 2, but eslint doesn't bug!");
console.log("I am a loooooooooo...ooooong line 3, but eslint doesn't bug!");
/* eslint-enable max-len */
console.log("I am a loooooooooo...ooooong line 4, AND eslint's GONNA CRY!"); // <--- eslint is gonna cry over this one only!

Case 2.2: You want to disable "Some Rules" for "One File"

  • Put the /* eslint-disable no-use-before-define */ comment at the top of the file.

More examples here.


Case 2.3: You want to disable "Some Rules" for "Some files"

  • This is less straightforward. You should create an "overrides" section in your .eslintrc and specify which rules should be disabled/modified for which globs/files. An example can be found in this answer.
Aidin
  • 25,146
  • 8
  • 76
  • 67
  • 42
    PS. Case 5: You want to disable "All rules" for "All Files" -- `npm uninstall eslint`! :p – Aidin Jun 22 '19 at 23:42
  • 7
    Also note that you can only use `/* */` style comment for `eslint-disable`. `// eslint-disable no-use-before-define` is not gonna work. It works for `// eslint-disable-next-line` though. – Aidin Nov 01 '19 at 23:07
  • 2
    I was needing exactly the **Case 2.3** during the time I was reading your comment and it is not completely explained on your answer. For anyone who want to know the **Case 2.3**, it is fully explained here: https://stackoverflow.com/a/65069069/7644846 . PS: `"excludedFiles"` doesn't work if you want to disable specific rules for specific files. – Victor Aug 29 '21 at 06:18
  • 1
    Thanks @victor for bringing it to my attention. It seems like eslint documentation links had changed. I updated them, and also made a reference to your answer for 2.3. :) Thanks! – Aidin Sep 01 '21 at 20:06
  • 1
    +1 being new to TypeScript, this eslint thing was driving me nuts but I didn't want to turn it off since many of the warnings were useful. This answer saved the day! – Eric Mutta Nov 01 '21 at 22:04
243

You can tell ESLint to ignore specific files and directories by creating an .eslintignore file in your project’s root directory:

.eslintignore

build/*.js
config/*.js
bower_components/foo/*.js

The ignore patterns behave according to the .gitignore specification. (Don't forget to restart your editor.)

prograhammer
  • 20,132
  • 13
  • 91
  • 118
  • 38
    Is it possible to ignore only specific rules in this file? – J. Hesters Feb 19 '19 at 17:03
  • 2
    Very nice! You can also add files in directories, subdirectories, subsubdirectories in a recursive way using **. For example: `spec/javascripts/**/*.js`. – Victor Dec 04 '19 at 19:51
  • Perfect - This is particularly useful for files that are auto-generated and changed frequently (like aws-exports.js if you're using AWS's Amplify or MobileHub) – bogan27 Mar 20 '20 at 01:04
  • 1
    This still gives a warning – dan-klasson Apr 02 '20 at 08:02
  • @dan-klasson That's incorrect. I've used this approach for years. Unless you are referring to the case where you explicitly pass a file into the eslint command and it warns you that you ignored this. But most all cases are where you specify a directory such as `eslint .` or `eslint src/`. You will NOT see warnings for ignored files that way. – prograhammer Apr 03 '20 at 08:28
  • It's also confirmed here in their documentation: https://eslint.org/docs/2.13.1/user-guide/configuring#ignored-file-warnings `When you pass directories to ESLint, files and directories are silently ignored. If you pass a specific file to ESLint, then you will see a warning indicating that the file was skipped.` – prograhammer Apr 03 '20 at 08:36
  • It's a git commit hook. I don't know how it's run. But I did get a warning. – dan-klasson Apr 03 '20 at 08:43
  • be careful about running eslint with `--ignore-path` argument. it would bypasses `.eslintignore` file content. – Ali Ghanavatian Sep 27 '20 at 09:38
  • 1
    This answer disable **all** rules from those files. In order to remove **specifics** rules from files, you ca check on my answer https://stackoverflow.com/a/65069069/7644846 – Victor Nov 30 '20 at 17:49
  • @J.Hesters Yes! It is possible to remove **specific** rules from files by using the `"overrides"` key. You can check that on my answer right here: stackoverflow.com/a/65069069/7644846 – Victor Nov 30 '20 at 17:50
  • `*.min.*` will ignore minified files – Washington Guedes Dec 13 '20 at 11:43
  • @J.Hesters keep scrolling to find my answer at https://stackoverflow.com/a/56719951 for disabling specific file(s) and/or rule(s). – Aidin Dec 12 '21 at 05:28
135

You can also disable/enable a rule like this:

/* eslint-disable no-use-before-define */
... code that violates rule ...
/* eslint-enable no-use-before-define */

Similar to eslint-disable-line as mentioned in the question. It might be a better method if you don't want to have to restore a complicated rule configuration when re-enabling it.

eppsilon
  • 2,031
  • 1
  • 16
  • 20
  • I Prefer this one, because it restores the level (warn, error) that I have defined in my eslint config. – Jankapunkt Jan 16 '18 at 14:35
  • This is the solution that worked for me. Turning the rule off and on didn't work. – JoshJoe Aug 24 '18 at 14:49
  • The problem with having this pattern in your codebase is, future developers who come to do this on their own files will forget re-enabling it. (It's not easy to see the difference when you don't re-enable.) That's why I personally prefer mostly `// esint-disable-line` or `/* eslint-disable-next-line */` which is for one line only. Find them with more details at my answer: https://stackoverflow.com/a/56719951/2321594 – Aidin Dec 12 '21 at 05:31
125

It's better to add "overrides" in your .eslintrc.js config file. For example if you wont to disable camelcase rule for all js files ending on Actions add this code after rules scope in .eslintrc.js.

"rules": {    
...........    
},
"overrides": [
 {
  "files": ["*Actions.js"],
     "rules": {
        "camelcase": "off"   
     }
 }
]
Andriy
  • 1,981
  • 1
  • 12
  • 9
  • It does not answer OP's question about specific file. – wscourge Mar 15 '19 at 14:16
  • 4
    Works great! IMHO this should be the accepted answer. This way stays in eslintrc file (keeps source files clean) and can override behavior of specific rules for specific files. +1 – akagixxer Jan 23 '20 at 16:24
  • Works well for a `*.d.ts` file so that you can stop linting warnings on type definitions for things like `no-unused-vars` or `max-classes-per-file` or `no-use-before-define` – Developer Dave Feb 09 '21 at 19:13
  • @akagixxer but you don't have control over one instance (i.e. line) with this method. OTOH, putting it in the code itself makes more visible to everyone that you are doing a "bad" thing, but an exception is justified. Also, my answer at https://stackoverflow.com/a/56719951 contains different ways that you can do it. – Aidin Dec 12 '21 at 05:34
  • ignoring lint rules in the source code in practice just provides patterns for developers in the future to ignore rules, and inevitably, legitimate rules get ignored. +1 for using overrides for legitimate exceptions to your ruleset +1 for this being the accepted answer – Brian Zitzow Aug 09 '22 at 17:54
70

To temporarily disable rule warnings in your file, use block comments in the following format:

/* eslint-disable */

This will disable ESLint until the

/* eslint-enable */

comment is reached.

You can read more about this topic here.

Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
samuel luswata
  • 808
  • 6
  • 4
68

To disable specific rules for file(s) inside folder(s), you need to use the "overrides" key of your .eslintrc config file.

For example, if you want to remove the following rules:

  1. no-use-before-define
  2. max-lines-per-function

For all files inside the following main directory:

/spec

You can add this to your .eslintrc file...

"overrides": [
  {
    "files": ["spec/**/*.js"],
    "rules": {
      "no-use-before-define": ["off"],
      "max-lines-per-function": ["off"]
    }
  }
]

Note that I used ** inside the spec/**/*.js glob, which means I am looking recursively for all subfolders inside the folder called spec and selecting all files that ends with js in order to remove the desired rules from them.

Victor
  • 1,904
  • 18
  • 18
  • IMO this approach is gonna make it harder to maintain. The future developer who wants to know why a certain rule is disabled, has to look in so many places. It might be easier to either centralize the rule-overrides in one place (one `.eslintrc`/`.eslintignore`) or do it per line/file, or both. – Aidin Aug 27 '21 at 19:34
  • 4
    I didn't understand your concern because this approach is exactly centralizing the rule-override inside one place: the `.eslintrc` file. – Victor Aug 29 '21 at 06:12
  • 1
    I see. I was under the impression that you are talking putting multiple `.eslintrc` files in different folders. My bad. :) – Aidin Dec 12 '21 at 05:43
  • 1
    This approach is far better than using comments. Code should never contain info about external things related to the build process. – Josh M. Feb 10 '23 at 16:38
37

Accepted answer didn't work for me (maybe a different version of eslint...? I'm using eslint v3.19.0), but did find a solution with the following...

Place the following on the top of your file

/* eslint-disable no-use-before-define */

This will disable the rule for the entire file

sfletche
  • 47,248
  • 30
  • 103
  • 119
  • 8
    ...or multiple, like `/* eslint-disable no-unused-vars, no-console, import/no-extraneous-dependencies, spaced-comment */` – Frank N Sep 29 '17 at 13:17
  • 1
    same same, accepted answer didn't work for me. Two differences: changing `eslint` to `eslint-disable` and dropping `: 0`. Using eslint@4.19.1. – hellatan Jun 22 '18 at 23:18
31
/* eslint-disable */

//suppress all warnings between comments
alert('foo');

/* eslint-enable */

This will disable all eslint rules within the block.

xy2
  • 6,040
  • 3
  • 14
  • 34
Shakil Ahmed
  • 1,481
  • 2
  • 19
  • 26
  • 8
    This will disable all eslint rules within that block, not just the one `no-use-before-define` intended to ignore – Jeremy Oct 19 '16 at 10:22
  • This helped me, thanks! Also consider this answer the question, disabling all rules also disable the intended rule – Fabiano Soriani Jan 25 '17 at 14:49
  • Really not a good practice. Just because you want to do one "unorthodox" thing, shouldn't justify a total chaos. :) See my answer at https://stackoverflow.com/a/56719951 for more granularity. – Aidin Dec 12 '21 at 05:37
17

Simple and effective.

Eslint 6.7.0 brought "ignorePatterns" to write it in .eslintrc.json like this example:

{
    "ignorePatterns": ["fileToBeIgnored.js"],
    "rules": {
        //...
    }
}

See docs

fjplaurr
  • 1,818
  • 3
  • 19
  • 36
  • Note that it ignores "ALL" the rules for the "ENTIRE" file. If you want more granularity, check my an answer at https://stackoverflow.com/a/56719951 – Aidin Dec 12 '21 at 05:43
13

If you want to disable ESLint for one rule, you can add this to the top of the file:

/* eslint-disable NAME_OF_THE_RULE */

If you want to disable ESLint or TypeScript checks inside a file, you can add these lines at the top of the file. The first one will disable TypeScript checks, and the second one ESLint checks.

// @ts-nocheck
/* eslint-disable */
Timbergus
  • 3,167
  • 2
  • 36
  • 35
Muhammed Moussa
  • 4,589
  • 33
  • 27
11

Just add this to the top of your file. This disables all eslint warnings respectively.

/* eslint-disable */
Druv
  • 143
  • 1
  • 2
9

you can configure eslint overrides property to turn off specific rules on files which matches glob pattern like below.

Here, I want to turn off the no-duplicate-string rule for tests all files.

overrides: [
  {
    files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
    rules: {
      'sonarjs/no-duplicate-string': 'off'
    }
  }
]
Sagar
  • 4,473
  • 3
  • 32
  • 37
8

You can just put this for example at the top of the file:

/* eslint-disable no-console */
OZZIE
  • 6,609
  • 7
  • 55
  • 59
6

As of today, the answer does not work for me, but putting this at the top of the file does work:

/* eslint-disable @typescript-eslint/no-unused-vars */

It is important to know that at least in my case, the type of comment makes a difference. The previous comment works for me, but the following won't work:

// eslint-disable @typescript-eslint/no-unused-vars
Javi Marzán
  • 1,121
  • 16
  • 21
1

You can turn off specific rule for a file by using /*eslint [<rule: "off"], >]*/

/* eslint no-console: "off", no-mixed-operators: "off" */

Version: eslint@4.3.0

Mahesh
  • 3,727
  • 1
  • 39
  • 49
1

Simply create an empty file .eslintignore in your project root the type the path to the file you want it to be ignore.

Source: https://eslint.org/docs/2.13.1/user-guide/configuring#:~:text=To%20disable%20rule%20warnings%20in,*%2F%20alert('foo')%3B

Line Ignoring Files and Directories

Napster Scofield
  • 1,271
  • 14
  • 13
  • You will not have control over specific rules with this, neither specific lines. My answer at https://stackoverflow.com/a/56719951 covers more cases, based on the need. – Aidin Dec 12 '21 at 05:35
  • I appreciate this was in case you want to ignore warnings in a specific file – Napster Scofield Jan 07 '23 at 20:17
0

To temporarily disable rule warnings in your file, use block comments in the following format:

/* eslint-disable */

alert('foo');

/* eslint-enable */ You can also disable or enable warnings for specific rules:

/* eslint-disable no-alert, no-console */

alert('foo'); console.log('bar');

/* eslint-enable no-alert, no-console / To disable rule warnings in an entire file, put a / eslint-disable */ block comment at the top of the file:

/* eslint-disable */

alert('foo'); You can also disable or enable specific rules for an entire file:

/* eslint-disable no-alert */

alert('foo'); To disable all rules on a specific line, use a line comment in one of the following formats:

Following are some examples to disable ESLint for a page

alert('foo'); // eslint-disable-line

// eslint-disable-next-line alert('foo'); To disable a specific rule on a specific line:

alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert alert('foo'); To disable multiple rules on a specific line:

alert('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi alert('foo');


Do following steps to disable ESLint from your project

  • open package.config file in your project.
  • remove all dependencies related to ESLint.
  • remove eslint.js/eslintconfig files from your project
  • run command npm install
  • now run your project
  • 1
    The original post asked how to disable it for a _single_ file. Not the entire project. – Johnathon Sullinger Sep 17 '21 at 22:00
  • You can use one backtick (the character next to 1) to format one span, or triple-backtick to format one block. See https://stackoverflow.com/editing-help Spending a few minutes formatting your answer can be more helpful and also have higher chance of receiving upvotes. :) – Aidin Dec 12 '21 at 05:47