[javascript] Turning off eslint rule for a specific file

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.

This question is related to javascript configuration lint eslint

The answer is


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

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


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.


Simple and effective.

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

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

See docs


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.


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

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

Version: [email protected]


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

/* eslint-disable no-console */

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


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


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.


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.)


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

/* eslint-disable */

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

/* eslint-enable */

This will disable all eslint rules within the block.


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

Based on the number of rules you want to ignore (All, or Some), and the scope of disabling it (Line(s), File(s), Everywhere), we have 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.


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 straight-forward. You should put them in "excludedFiles" object of "overrides" section of your .eslintrc as instructed here.

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to configuration

My eclipse won't open, i download the bundle pack it keeps saying error log Getting value from appsettings.json in .net core pgadmin4 : postgresql application server could not be contacted. ASP.NET Core configuration for .NET Core console application Turning off eslint rule for a specific file PHP Warning: Module already loaded in Unknown on line 0 How to read AppSettings values from a .json file in ASP.NET Core How to store Configuration file and read it using React Hadoop cluster setup - java.net.ConnectException: Connection refused Maven Jacoco Configuration - Exclude classes/packages from report not working

Examples related to lint

SyntaxError: unexpected EOF while parsing Turning off eslint rule for a specific file Permission is only granted to system app JSHint and jQuery: '$' is not defined What is "Linting"?

Examples related to eslint

How to fix missing dependency warning when using useEffect React Hook? ESLint not working in VS Code? No restricted globals eslint: error Parsing error: The keyword 'const' is reserved Disable eslint rules for folder React eslint error missing in props validation Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style ESLint Parsing error: Unexpected token Turning off eslint rule for a specific file Eslint: How to disable "unexpected console statement" in Node.js?