Error: No ESLint configuration found.
Ctrl+Alt+s -> Languages & Frameworks -> Code Quality Tools -> ESLint -> Enable
,将 Enable
前面的复选框设置为非勾选状态。ESLint
(二选一),并运行 eslint --init
npm i eslint --save-dev
,如果是开源项目,推荐这种方式。npm i eslint -g
eslint --init
。如果用 WebStorm 打开的项目,直接在 Terminal
窗口执行命令即可(默认路径就是项目根目录)。通过以上操作,最终在项目根目录下生成文件 .eslintrc.yml
,内容如下:
env: browser: true commonjs: true es6: true extends: 'eslint:recommended' parserOptions: ecmaFeatures: jsx: true ecmaVersion: 2018 sourceType: module plugins: - react rules: # 缩进风格:这里设置的是用4个空格缩进。 # 至于要用几个空格来缩进,需要根据团队的编码规范来。 # 如果是研究别人的开源项目,建议根据开源项目的实际情况来设置。 indent: - error - 4 linebreak-style: - error - windows quotes: # 引号:设置成双引号 - error - double semi: # 分号:语句末尾需要有分号 - error - always
WebStorm
如何配置ESLint
是配置到 package.json
,即仅项目下安装,而不是全局安装,则 Configuration file
,勾选 Automatic search
即可。ESLint
是全局安装,则要指定配置文件,勾选 Configuration file:
并在其后指定配置文件。eslint-plugin-react
依赖在执行 eslint --init
初始化配置文件的时候,如果选择了要使用 react,则需要安装依赖 eslint-plugin-react
,否则会提示: Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react'
。
解决此类问题,最基本的原则是,缺什么依赖,就安装什么依赖。只是要注意区分,是全局安装还是仅项目安装。
eslint-plugin-react
是否全局安装,与 ESLint
是否全局安装相关:
ESLint
是全局安装的,eslint-plugin-react
也要全局安装,安装命令: cnpm install eslint-plugin-react -g
。ESLint
只是在项目下安装,安装命令: cnpm install eslint-plugin-react --save-dev
。关于 ESLint
与 eslint-plugin-react
的搭配安装,官方文档是这样描述的:If you installed ESLint globally, you have to install React plugin globally too. Otherwise, install it locally.