eslint遇到的报错

  • A+
所属分类:Web前端
摘要

eslint 和prettier ,如果有语法错误,都会阻止代码自动格式化 相当于,需要你先解决掉语法错误之后,再自动给你格式化

eslint 和prettier ,如果有语法错误,都会阻止代码自动格式化

相当于,需要你先解决掉语法错误之后,再自动给你格式化

配置错误

  • eslint Parsing error: ecmaVersion must be 3, 5, 6, or 7:

解决方案:https://stackoverflow.com/questions/62903921/how-do-i-fix-eslint-createrequire-is-not-a-function-in-atom-editor

 

  • npx eslint TypeError: Module.createRequire is not a function

解决方案:https://stackoverflow.com/questions/62903921/how-do-i-fix-eslint-createrequire-is-not-a-function-in-atom-editor

原因:我使用的node版本v10.19.0node版本过低
解决办法:

  1. node版本升级到12以上
  2. 将 eslint 降级到版本6.8.0(7.0.0 之前的最后一个版本)。此版本仍包含createRequire.
  • Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/wangping/learn/test-eslint/node_modules/@eslint/eslintrc/universal.js

require() of ES modules is not supported.

require() of /Users/wangping/learn/test-eslint/node_modules/@eslint/eslintrc/universal.js from /Users/wangping/learn/test-eslint/node_modules/eslint/lib/linter/linter.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.

Instead rename universal.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/wangping/learn/test-eslint/node_modules/@eslint/eslintrc/package.json.

https://python.iitter.com/other/270726.html

原因:还是node版本

解决办法:上个问题我改为使用了node v12.16.1 版本,根据官网(https://eslint.org/docs/user-guide/getting-started)介绍,node版本需要^12.22.0, ^14.17.0, or >=16.0.0才行。

  • npx eslint --ext .js,.vue 执行没反应,也不报错

原因:npx eslint . --ext .js,.vue 后缀名可加双引号、单引号、 可不加,前面的点必须得加,不加执行没反应,也不报错。

 

语法错误

  • The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

解决方案:

https://stackoverflow.com/questions/4166551/javascript-jslint-error-the-body-of-a-for-in-should-be-wrapped-in-an-if-statem

https://stackoverflow.com/questions/1963102/what-does-the-jslint-error-body-of-a-for-in-should-be-wrapped-in-an-if-statemen

  • Error: Failed to load parser '@babel/eslint-parser' declared in '.eslintrc.js » eslint-config-alloy » ./base.js': Cannot find module '@babel/eslint-parser'

原因:安装了 eslint-config-alloy 之后,会报此错误

解决办法: npm instasll -D @babel/eslint-parser 即可

 

 

其余eslint场景问题可参考网友文章:https://juejin.cn/post/7024760884421623822