为什么我的Ionic React应用程序linter不产生任何输出?

gpnt7bae  于 2023-11-15  发布在  Ionic
关注(0)|答案(2)|浏览(163)

bounty将在2天内过期。回答此问题可获得+50声望奖励。Dave正在寻找来自信誉良好的来源的**答案 *。

我正在构建一个Ionic 7,React 18应用程序。我想偶尔运行linting,使用eslint,但目前没有得到任何输出。

$ npm run lint

> [email protected] lint
> eslint

字符串
这是我的package.json

{
  "name": "frontend",
  "private": true,
  "version": "0.0.1",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview",
    "test.e2e": "cypress run",
    "test.unit": "vitest",
    "lint": "eslint"
  },
  "dependencies": {
    "@capacitor/app": "5.0.6",
    "@capacitor/core": "5.5.0",
    "@capacitor/haptics": "5.0.6",
    "@capacitor/keyboard": "5.0.6",
    "@capacitor/status-bar": "5.0.6",
    "@ionic/react": "^7.0.0",
    "@ionic/react-router": "^7.0.0",
    "@types/react-router": "^5.1.20",
    "@types/react-router-dom": "^5.3.3",
    "axios": "^1.5.1",
    "dotenv": "^16.3.1",
    "ionicons": "^7.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router": "^5.3.4",
    "react-router-dom": "^5.3.4",
    "swiper": "^10.3.1"
  },
  "devDependencies": {
    "@capacitor/cli": "5.5.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^14.0.0",
    "@testing-library/user-event": "^14.4.3",
    "@types/react": "^18.0.27",
    "@types/react-dom": "^18.0.10",
    "@typescript-eslint/eslint-plugin": "^6.9.0",
    "@typescript-eslint/parser": "^6.9.0",
    "@vitejs/plugin-legacy": "^4.0.2",
    "@vitejs/plugin-react": "^4.0.1",
    "cypress": "^12.7.0",
    "eslint": "^8.52.0",
    "eslint-config-react-app": "^7.0.1",
    "eslint-plugin-react": "^7.33.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-unused-imports": "^3.0.0",
    "jsdom": "^22.1.0",
    "typescript": "^5.1.6",
    "vite": "^4.3.9",
    "vitest": "^0.32.2"
  },
  "description": "An Ionic project"
}


这是我的. eslintrc.js文件

module.exports = {
  root: true,
  env: {
    node: true,
    es6: true,
  },
  extends: ['react-app'],
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    "unused-imports/no-unused-imports": "error",
    "unused-imports/no-unused-vars": [
      "warn",
      {
        "vars": "all",
        "varsIgnorePattern": "^_",
        "args": "after-used",
        "argsIgnorePattern": "^_"
      }
    ]
  },
};


我还需要配置什么才能让我的linter运行?

**编辑:**添加错误输出以响应给定的答案...

> [email protected] lint
> eslint --fix *.{ts,tsx,json,md} src/** lib/**

Oops! Something went wrong! :(

ESLint: 8.52.0

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/davea/Documents/workspace/myproject/frontend/.eslintrc.js from /Users/davea/Documents/workspace/myproject/frontend/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 /Users/davea/Documents/workspace/myproject/frontend/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at module.exports [as default] (/Users/davea/Documents/workspace/myproject/frontend/node_modules/import-fresh/index.js:32:59)
    at loadJSConfigFile (/Users/davea/Documents/workspace/myproject/frontend/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2574:47)
    at loadConfigFile (/Users/davea/Documents/workspace/myproject/frontend/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2658:20)
    at ConfigArrayFactory.loadInDirectory (/Users/davea/Documents/workspace/myproject/frontend/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2868:34)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/Users/davea/Documents/workspace/myproject/frontend/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3860:46)
    at CascadingConfigArrayFactory.getConfigArrayForFile (/Users/davea/Documents/workspace/myproject/frontend/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3781:18)
    at FileEnumerator._iterateFilesWithFile (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/cli-engine/file-enumerator.js:368:43)
    at FileEnumerator._iterateFiles (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/cli-engine/file-enumerator.js:349:25)
    at FileEnumerator.iterateFiles (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/cli-engine/file-enumerator.js:299:59)
    at iterateFiles.next (<anonymous>)
    at CLIEngine.executeOnFiles (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/cli-engine/cli-engine.js:797:48)
    at ESLint.lintFiles (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/eslint/eslint.js:551:23)
    at Object.execute (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/lib/cli.js:402:36)
    at async main (/Users/davea/Documents/workspace/myproject/frontend/node_modules/eslint/bin/eslint.js:146:24)


$ npm run lint

> [email protected] lint
> eslint --fix *.{ts,tsx,json} src/** lib/**

=============

WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: >=3.3.1 <5.2.0

YOUR TYPESCRIPT VERSION: 5.2.2

Please only submit bug reports when using the officially supported version.

=============

Oops! Something went wrong! :(

ESLint: 8.52.0

No files matching the pattern "*.tsx" were found.
Please check for typing mistakes in the pattern.


请注意,实际上有许多文件以“.tsx”结尾。

$ ls src/pages/
ConfirmEmailPage.tsx  Home.tsx              LoginPage.tsx         OrderSender.tsx       
Home.css              Home2.tsx             OrderSender.css       SignUpPage.tsx

guz6ccqo

guz6ccqo1#

我认为你在package.json中的命令需要告诉eslint它应该lint哪些文件。如果你不告诉它去哪里看,它永远不会习惯所有的东西。
试着把它改成这样:

"lint": "eslint *.js src/* --color",

字符串

"lint": "eslint --fix *.{js,json,md} src/** lib/**",

b09cbbtk

b09cbbtk2#

您的package.json中的type:"module"设置之间存在冲突。eslint 试图将您的.eslintrc.js加载为CommonJS模块,并抛出错误,因为它期望文件使用import语句而不是require()Reference
你应该像@大卫说的那样做。但是当你改变和错误的时候..
因为这是一个React/Ionic项目,文件扩展名是Typescript。即使如此,将我的命令更改为'“lint”:“eslint --fix .{ts,tsx,json,md} src/* lib/"'也会导致错误。
然后,确保将.eslintrc.js文件重命名为.eslintrc.cjs,以便eslint将文件视为
CommonJS并允许您使用require()
请确保您的
lint**配置如下所示:

"scripts": {
    ...
    "lint": "eslint --fix *.{ts,tsx,json,md} src/** lib/**",
    ...
   }

字符串
如果还是错误,也许你应该换一种方式,所以尝试放弃上面的设置,并将类型从"type": "module"改为"type": "commonjs",将所有.js文件视为CommonJS。这也意味着使用import加载其他 js 文件。

相关问题