AlloyTeam / eslint-config-alloy

Progressive ESLint config for your React/Vue/TypeScript projects
https://alloyteam.github.io/eslint-config-alloy/
2.65k stars 317 forks source link

What's the difference between `.eslintrc.js` and `.eslintrc.cjs`? #235

Closed RSS1102 closed 1 year ago

RSS1102 commented 1 year ago

.eslintrc.js`:

[Info  - 05:00:00] ESLint server is starting
[Info  - 05:00:00] ESLint server running in node v16.14.2
[Info  - 05:00:00] ESLint server is running.
[Info  - 05:00:02] ESLint library loaded from: C:\Users\lenovo\Desktop\react-init-template\node_modules\eslint\lib\api.js
[Error - 05:00:02] Cannot read config file:
 C:\Users\lenovo\Desktop\react-init-template\.eslintrc.js Error: require() of ES Module 
C:\Users\lenovo\Desktop\react-init-template\.eslintrc.js from C:\Users\lenovo\Desktop\react-init-
template\node_modules\@eslint\eslintrc\dist\eslintrc.cjs not supported. 
.eslintrc.js is 
treated as an ES module file as it is a .js file whose nearest parent package.json 
contains "type": "module" which declares all .js files 
in that package scope as ES modules.
 Instead rename .eslintrc.js to end in .cjs, change the requiring code to use dynamic import() 
which is available in all CommonJS 
modules, or change "type": "module" to "type": "commonjs" in 
C:\Users\lenovo\Desktop\react-init-template\package.json to treat all .js 
files as CommonJS (using .mjs for all ES modules instead). 

use .eslintrc.cjs,I succeeded in getting the correct eslint warning. Then I changed it to .eslintrc.js again and still got the correct elslint warning.

I don't understand why. Or i changed "type": "module" in package.json. Why do I change it? Will it affect me?

xcatliu commented 1 year ago

没看明白你的意思。看日志意思是说 "type": "module" 会把所有 .js 都作为 es module 解析,所以不能在其中使用 require 语法。

RSS1102 commented 1 year ago

没看明白你的意思。看日志意思是说 "type": "module" 会把所有 .js 都作为 es module 解析,所以不能在其中使用 require 语法。

是的,我看到这个日志也是这样认为的(eslint日志推荐我使用.cjs结尾的文件)。所以我不知道为啥会出现这个错误提示(即使我去掉了package.json内的"type": "module"),您没有遇到这种情况嘛。

xcatliu commented 1 year ago

参考这个吧 https://stackoverflow.com/questions/70487806/must-use-import-to-load-es-module-eslintrc-js

RSS1102 commented 1 year ago

参考这个吧 https://stackoverflow.com/questions/70487806/must-use-import-to-load-es-module-eslintrc-js

ok,谢谢你的解答,我似乎明白了一些。不管怎样,我正确运行了它,并且得到我期望的效果。

看到eslint官网的一段话,补充一下: JavaScript (ESM) - use .eslintrc.cjs when running ESLint in JavaScript packages that specify "type":"module" in their package.json. Note that ESLint does not support ESM configuration at this time.

再次感谢您的耐心解答。