I don't have an .eslintignore file and .eslintrc has no ignore patterns. When I run the following: ./node_modules/.bin/eslint **/*.js, it appears that files in the node_modules folder are ignored.
The ESLint documentation makes no mention of any default excluded folders or files. What is the real situation?
node_modules is ignored by default
It is in the doc: https://eslint.org/docs/latest/user-guide/configuring/
relevant part:
In addition to any patterns in the .eslintignore file, ESLint always
follows a couple of implicit ignore rules even if the --no-ignore flag
is passed. The implicit rules are as follows:
node_modules/ is ignored.
dot-files (except for .eslintrc.*), as well as dot-folders and their contents, are ignored.
Related
let's say, I have following project structure:
back/package.json
back/lib/Content/*.js
front/package.json
slices/budget/back/package.json
slices/budget/back/lib/Content/*.js
slices/budget/front/package.json
slices/accounting/back/package.json
slices/accounting/back/lib/Content/*.js
slices/accounting/front/package.json
how do I?
cd back && eslint ./lib/**/*.js ../slices/**/lib/Content/*.js
specifically, I want to
install eslint one time as devDependencies
somewhere in /back of root module
config eslint one time somewhere in /back/package.json:eslint key of root module
add eslint config in /back/package.json of root module just one time
eslint entire tree of modules
not in each slice seperatly
run from ci cd
so I need a way to run from /back
and later - maybe someway to respect eslint config hierarchy
not change project directory structure at all
what I receive
cd back && npm run lint
> back#1.0.0 lint
> eslint ../
Oops! Something went wrong! :(
ESLint: 8.23.1
ESLint couldn't find a configuration file
reason: https://eslint.org/docs/latest/user-guide/configuring/configuration-files#using-configuration-files
You can use the --ignore-path option to specify a file with patterns that should be ignored. The file should contain one pattern per line. For example, to ignore all files in the node_modules directory, you could create a .eslintignore file with the following contents:
node_modules
You can also use the --ignore-pattern option to specify a pattern that should be ignored. For example, to ignore all files in the node_modules directory, you could run:
eslint . --ignore-pattern node_modules
The error is probably because you haven't specified the eslint config file explicitly. To run eslint on all the modules, starting from the parent folder, run: eslint ../ -c .eslintrc.js (or whatever .eslintrc file you use in back). It seems like eslint is confused if it does not have the config file in the same directory it is running from hence you need to manually specify the path to it.
The correct way of solving this issue would be creating sharable config file with configuration you have in back right now:
module.exports = {
rules: {
semi: [2, "always"]
}
};
Then you publish it to public or private npm with a name #your-project/eslint-config and use it in .eslintrc.json that is the same in all your projects:
{
"extends": [
"#your-project/eslint-config"
]
}
This way gives you ability to configure CI in a simple and independent way if you have lots of repositories: just run eslint lib/*.js.
If you have all the repositories in one computer and want to lint all of them using one command, you can use one of my tools:
redfork, install eslint and redfork globally and run:
redfork 'eslint lib/*.js'
But maybe you need to have some changes in project structure.
runny, if you don't want to make changes in project structure, just add configuration file .runny.json:
{
"command": "eslint lib/*.js",
"directories": [
"~/one",
"~/two",
"~/three"
]
}
It will run the same command for any directory you need.
I had a similar issue and the following has solved my problem.
I guess you haven't specified the eslint config file explicitly.
To run eslint on all the modules
run: eslint ../ -c .eslintrc.js
It seems like eslint is confused if it does not have the config file in the same directory it is running from, so you need to manually specify the path to it.
no real answer, except to create .eslintignore, .eslintrc, package.json at project root
I have installed prettier via
yarn add prettier
I would like prettier to only format typescript code (I am developing AWS CDK project, there is no src folder by convention & there could be typescript files here and there). In other words, I would like prettier to check all files in my project that has extension *.ts.
I checked its documentation for configuration. But there is no such option to specify file extension.
How can I run prettier for only *ts files then? Is it even possible? If not, what could be the workaround?
To exclude files from formatting, create a .prettierignore file at the root of your project.
And to format only the *.ts files you should ignore everything but the *.ts files.
Example
# Ignore everything recursively
*
# But not the .ts files
!*.ts
# Check subdirectories too
!*/
In the code above, the * means to ignore everything including the subfolders, and the next line !*.ts tells the prettier to reverse the previous ignoring of the .ts files. The last line !*/ means to check the subdirectories too, but with the previous rule, it's only looking for the .ts files.
Check the prettier and gitignore docs for more information.
For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
I am linting old NodeJS code and I want to clear out all indent issues before I start fixing the rest.
How do I configure ESlint to only show and fix the indent issues only?
By using eslint in the command line with the proper options:
node node_modules/eslint/bin/eslint --fix --parser babel-eslint --ext js --no-eslintrc --rule 'indent: [1,4,{SwitchCase: 1}]' src/
Change these options according to your requirements
--fix to auto fix.
--parser babel-eslint the parser from my eslint.rc.
--ext js to lint only js files.
--no-eslintrc otherwise all rules from your eslintrc will be executed.
--rule 'indent: [1,4,{SwitchCase: 1}]' the rule you want to execute (you can copy paste it from your eslint.rc, it has to be single quoted (the double quotes from the original json were removed).
src/ the target folder.
eslint documentation
If you want to use your normal .eslintrc file to keep your configuration (parser, plugins, rule config, etc), you can use eslint-nibble with the --rule= indent flag. This will respect your normal configuration, but only show you errors from that rule, and give you the option to fix only those, if the rule is auto-fixable.
Disclaimer: I'm the creator of eslint-nibble.
I've placed a .eslintignore file in the root of my create-react-app installation, so I can ignore warnings from vendor libs.
It works fine if I run eslint from the commandline:
./node_modules/.bin/eslint src/js/vendor/jquery.js
...warnings ignored...
However running npm start and npm run build seem to ignore the ignore file.
How can I achieve what I want to do here - without editing individual vendor files to add eslint options?
This was fixed with this commit in CRA codebase: https://github.com/facebook/create-react-app/commit/6f5221c2d7df0b7a097cfdd461967422c3013e12#diff-dc0c4e7c623b73660da1809fc60cf6ba
Simply add EXTEND_ESLINT=true to a .env file in your project root.
YES .eslintignore is ignored. According to this issue.
CRA 1.x+ purposely does not support .eslintignore. Sorry!
Workarround
add /* eslint-disable */ to the beginning of the file.
move vendor files into public/ or use a NPM package.
Seems to work if I exclude complete directories from the .eslintignore in the root. Here is my .eslintignore
src/js/vendor/
src/vendor/
I'm working on a project that has an .eslintrc file checked in to source control at the root of the project.
The problem is that it has custom dependencies and when I use a tool to run my linting (syntastic with Vim) it fails. The dependencies are custom and I wish to skip running them in my editor.
I want to use another .eslintrc file, rather than the one which is in the root of the project.
How can I ignore <projectroot>/.eslintrc and instead use /custom/path/.eslintrc?
A glance at the ESLint docs tells us that you want to pass the --no-eslintrc option to make it ignore the .eslintrc in the project root and --config /custom/path/.eslintrc to make it use that file instead.
The Syntastic README says you can configure the arguments given to eslint like so:
let g:syntastic_javascript_eslint_args = "--no-eslintrc --config /custom/path/.eslintrc"