简体   繁体   English

如何配置 package.json 来运行 eslint 脚本

[英]How to configure package.json to run eslint script

So I am using eslint as a linter for my react project and I would like it to check all of my .js files.所以我使用 eslint 作为我的 react 项目的 linter,我希望它检查我所有的.js文件。

I am able to do this through the script:我可以通过脚本做到这一点:

"lint": "eslint back/*.js && eslint backTest/*.js && eslint front/actions/*.js"

how can I get it to examine every .js file recursively, something like:我怎样才能让它递归地检查每个.js文件,例如:

"lint": "eslint -r *.js"

This would save me having to type out each file inidvidually这将使我不必单独输入每个文件

Thanks in advance for the help在此先感谢您的帮助

eslint "**/*.js" to run on all js files in all the folders recursively (in the current folder) eslint "**/*.js"递归地在所有文件夹中的所有js文件上运行(在当前文件夹中)

You can also do: AnyFolder/**/*.js你也可以这样做: AnyFolder/**/*.js

And to ignore a folder: eslint "**/*.js" --ignore-pattern node_modules/并忽略一个文件夹: eslint "**/*.js" --ignore-pattern node_modules/

Know more at eslint/command-line-interfaceeslint/command-line-interface了解更多信息

eslint . --ext .js eslint . --ext .js to lint files with the .js extension. eslint . --ext .js以 .js 扩展名 lint 文件。

The . . targets files in the current directory and all subdirectories.定位当前目录和所有子目录中的文件。

To include other file extensions, eslint . --ext .js,.jsx要包含其他文件扩展名, eslint . --ext .js,.jsx eslint . --ext .js,.jsx or eslint . --ext .js --ext .jsx eslint . --ext .js,.jsxeslint . --ext .js --ext .jsx eslint . --ext .js --ext .jsx . eslint . --ext .js --ext .jsx

Theeslint documentation covers this option. eslint 文档涵盖了这个选项。

I'm not sure if the accepted answer is outdated, but by looking at thedocs ,我不确定接受的答案是否已过时,但通过查看文档

By default, it uses .js as the only file extension.默认情况下,它使用 .js 作为唯一的文件扩展名。

Also, according to a member's comment on the project's Github, using .此外,根据该项目 Github 上一位成员的评论,使用. equals running in all subdirectories.等于在所有子目录中运行。 It seems to me that running eslint .在我看来,运行eslint . should suffice (though it doesn't cover the new ES Module .mjs files).应该足够了(尽管它不包括新的 ES Module .mjs文件)。

To add to TranBrian10's solution, I installed eslint locally, so calling eslint in the terminal results in a command not found error.为了添加到 TranBrian10 的解决方案,我在本地安装了 eslint,因此在终端中调用eslint会导致command not found错误。

I was able to get around this by using npx eslint instead:我可以通过使用npx eslint来解决这个问题:

`eslint . --ext .js` -> `npx eslint . --ext .js`

And as GollyJer noted, this won't work for Windows due to the .正如 GollyJer 指出的那样,由于. syntax句法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM