13

I am new in using Eslint.

So far I have installed Eslint in my local project and configured it. The eslintrc.js file contains

module.exports = {
  env: {
    node: true,
    commonjs: true,
    es6: true,
    mocha: true,
  },
  extends: [
    'airbnb-base',
  ],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parserOptions: {
    ecmaVersion: 2018,
  },
  rules: {
  },
};

And in package.json I have

  "scripts": {
    "lint": "eslint .eslintrc.js --fix",
}

In terminal I run

npm run lint

And the output is

> apigateway@1.0.0 lint C:\nodeprojects\restapi
> eslint .eslintrc.js --fix


C:\nodeprojects\restapi\.eslintrc.js
  0:0  warning  File ignored by default.  Use a negated ignore pattern (like "--ignore-pattern '!<relative/path/to/filename>'") to override

But if I run

eslint <foldername> --fix then it works.

I am using webstorm IDE and in windows os.

Any help is highly appreciated.

Prithviraj Mitra
  • 11,002
  • 13
  • 58
  • 99

3 Answers3

19

ESlint default behaviour is ignoring file/folders starting with . - https://github.com/eslint/eslint/issues/10341.

In case for example you want to lint .storybook folder, ESLint will ignore it by default. To lint it, .eslintrc.js must include:

{
  ...
  // Lint ".storybook" folder (don't ignore it)
  "ignorePatterns": ["!.storybook"],
  ...
}

Because of that default ESLint behaviour, I do in all my projects like this (lint whole project from the root) :

{
  ...
  // ESlint default behaviour ignores file/folders starting with "." - https://github.com/eslint/eslint/issues/10341
  "ignorePatterns": ["!.*", "dist", "node_modules"],
  ...
}

Where I first don't ignore any file/folder starting with . and then I exclude folders that I actually want to ignore.

marko424
  • 3,839
  • 5
  • 17
  • 27
6

Your npm script runs the linter on the .eslintrc.js file and this file is as the comment says File ignored by default.

You need to change the lint script from:

"lint": "eslint .eslintrc.js --fix",

to:

"lint": "eslint <foldername> --fix"

Where <foldername> is the correct folder.

Mac_W
  • 2,927
  • 6
  • 17
  • 30
  • Thanks a lot. I have changed to `"lint": "eslint . --fix"` and I believe that it will run on all the files in the current folder? If so then will it include node_modules folder too? Also, my second question is that then how .eslintrc.js file will be called or related in this whole process. – Prithviraj Mitra Sep 15 '19 at 20:53
  • You can create another file in your root called .eslintignore where you can place folders to be ignored like /node_modules/ in this case. – Mac_W Sep 16 '19 at 07:59
  • 2
    Also, eslint ignores `node_modules` and `bower_components` by default, so you don't need to do anything extra for those. – William Swanson Sep 30 '19 at 19:25
5

I ran into this issue where the pre-commit Husky hook would try to scan this file when trying to commit merges (where someone changed .eslintrc.js previously), then this would fail the merge commit in team members' local envs (file ignored by default)

You can create a .eslintignore file next to .eslintrc.js. Then in .eslintignore put:

!.eslintrc.js