【问题标题】:ESlint - Error: Must use import to load ES ModuleESlint - 错误:必须使用导入来加载 ES 模块
【发布时间】:2021-12-01 20:58:01
【问题描述】:

我目前正在使用 React、Typescript、样式化组件、webpack 等设置样板,但在尝试运行 eslint 时出现错误:

错误:必须使用 import 来加载 ES 模块

这是一个更详细的错误版本:

/Users/ben/Desktop/development projects/react-boilerplate-styled-context/src/api/api.ts
  0:0  error  Parsing error: Must use import to load ES Module: /Users/ben/Desktop/development projects/react-boilerplate-styled-context/node_modules/eslint/node_modules/eslint-scope/lib/definition.js
require() of ES modules is not supported.
require() of /Users/ben/Desktop/development projects/react-boilerplate-styled-context/node_modules/eslint/node_modules/eslint-scope/lib/definition.js from /Users/ben/Desktop/development projects/react-boilerplate-styled-context/node_modules/babel-eslint/lib/require-from-eslint.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 definition.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/ben/Desktop/development projects/react-boilerplate-styled-context/node_modules/eslint/node_modules/eslint-scope/package.json

我的每个 .js 和 .ts/ .tsx 文件中都会出现错误,我只使用 import 或者该文件根本没有导入。我明白错误在说什么,但我不知道为什么会抛出它,而实际上我在某些文件中只使用导入甚至根本不使用导入。

这是我的 package.json,我使用 npm run lint:eslint:quiet 触发 linter:

{
  "name": "my-react-boilerplate",
  "version": "1.0.0",
  "description": "",
  "main": "index.tsx",
  "directories": {
    "test": "test"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "type": "module",
  "scripts": {
    "build": "webpack --config webpack.prod.js",
    "dev": "webpack serve --config webpack.dev.js",
    "lint": "npm run typecheck && npm run lint:css && npm run lint:eslint:quiet",
    "lint:css": "stylelint './src/**/*.{js,ts,tsx}'",
    "lint:eslint:quiet": "eslint --ext .ts,.tsx,.js,.jsx  ./src --no-error-on-unmatched-pattern --quiet",
    "lint:eslint": "eslint --ext .ts,.tsx,.js,.jsx  ./src --no-error-on-unmatched-pattern",
    "lint:eslint:fix": "eslint --ext .ts,.tsx,.js,.jsx  ./src --no-error-on-unmatched-pattern --quiet --fix",
    "test": "cross-env NODE_ENV=test jest --coverage",
    "test:watch": "cross-env NODE_ENV=test jest --watchAll",
    "typecheck": "tsc --noEmit",
    "precommit": "npm run lint"
  },
  "lint-staged": {
    "*.{ts,tsx,js,jsx}": [
      "npm run lint:eslint:fix",
      "git add --force"
    ],
    "*.{md,json}": [
      "prettier --write",
      "git add --force"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "npx lint-staged && npm run typecheck"
    }
  },
  "resolutions": {
    "styled-components": "^5"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.5.4",
    "@babel/plugin-proposal-class-properties": "^7.5.0",
    "@babel/preset-env": "^7.5.4",
    "@babel/preset-react": "^7.0.0",
    "@types/history": "^4.7.6",
    "@types/react": "^17.0.29",
    "@types/react-dom": "^17.0.9",
    "@types/react-router": "^5.1.17",
    "@types/react-router-dom": "^5.1.5",
    "@types/styled-components": "^5.1.15",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "babel-cli": "^6.26.0",
    "babel-eslint": "^10.0.2",
    "babel-loader": "^8.0.0-beta.6",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "clean-webpack-plugin": "^4.0.0",
    "dotenv-webpack": "^7.0.3",
    "error-overlay-webpack-plugin": "^1.0.0",
    "eslint": "^8.0.0",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-with-prettier": "^6.0.0",
    "eslint-plugin-compat": "^3.3.0",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.14.2",
    "eslint-plugin-react-hooks": "^4.2.0",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.3.2",
    "husky": "^7.0.2",
    "prettier": "^2.4.1",
    "raw-loader": "^4.0.2",
    "style-loader": "^3.3.0",
    "stylelint": "^13.13.1",
    "stylelint-config-recommended": "^5.0.0",
    "stylelint-config-styled-components": "^0.1.1",
    "stylelint-processor-styled-components": "^1.10.0",
    "ts-loader": "^9.2.6",
    "tslint": "^6.1.3",
    "typescript": "^4.4.4",
    "url-loader": "^4.1.1",
    "webpack": "^5.58.2",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^4.3.1",
    "webpack-merge": "^5.3.0"
  },
  "dependencies": {
    "history": "^4.10.0",
    "process": "^0.11.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "styled-components": "^5.2.1"
  }
}

这是我的 .eslintrc 文件:

{
  "extends": ["airbnb", "prettier"],
  "parser": "babel-eslint",
  "plugins": ["prettier", "@typescript-eslint"],
  "parserOptions": {
    "ecmaVersion": 8,
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "impliedStrict": true,
      "classes": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "jest": true
  },
  "rules": {
    "arrow-body-style": ["error", "as-needed"],
    "class-methods-use-this": 0,
    "react/jsx-filename-extension": 0,
    "global-require": 0,
    "react/destructuring-assignment": 0,
    "import/named": 2,
    "linebreak-style": 0,
    "import/no-dynamic-require": 0,
    "import/no-named-as-default": 0,
    "import/no-unresolved": 2,
    "import/prefer-default-export": 0,
    "semi": [2, "always"],
    "max-len": [
      "error",
      {
        "code": 80,
        "ignoreUrls": true,
        "ignoreComments": true,
        "ignoreStrings": true,
        "ignoreTemplateLiterals": true
      }
    ],
    "new-cap": [
      2,
      {
        "capIsNew": false,
        "newIsCap": true
      }
    ],
    "no-param-reassign": 0,
    "no-shadow": 0,
    "no-tabs": 2,
    "no-underscore-dangle": 0,
    "react/forbid-prop-types": [
      "error",
      {
        "forbid": ["any"]
      }
    ],
    "import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
    "react/jsx-no-bind": [
      "error",
      {
        "ignoreRefs": true,
        "allowArrowFunctions": true,
        "allowBind": false
      }
    ],
    "react/no-unknown-property": [
      2,
      {
        "ignore": ["itemscope", "itemtype", "itemprop"]
      }
    ]
  }
}

我不确定是否相关,还有我的 tsconfig.eslint.json 文件:

{
  "extends": "./tsconfig.json",
  "include": ["./src/**/*.ts", "./src/**/*.tsx", "./src/**/*.js"],
  "exclude": ["node_modules/**", "build/**", "coverage/**"]
}

不确定以前是否有人遇到过这种情况?谷歌搜索错误不会显示任何有用的论坛或提出的错误,其中大多数只是声明不要在您的文件中使用require,而我不是。

【问题讨论】:

    标签: javascript node.js reactjs eslint


    【解决方案1】:

    我认为问题在于您正在尝试使用已弃用的babel-eslint parser,最近一次更新是一年前,看起来它不支持 ES6 模块。更新到 latest parser 似乎有效,至少对于简单的 linting。

    所以,这样做:

    • 在 package.json 中,将行 "babel-eslint": "^10.0.2", 更新为 "@babel/eslint-parser": "^7.5.4",。这适用于上面的代码,但最好使用最新版本,在撰写本文时为 7.16.3。
    • 从文件夹中的终端/命令提示符运行npm i
    • 在 .eslintrc 中,将解析器行 "parser": "babel-eslint", 更新为 "parser": "@babel/eslint-parser",
    • 在 .eslintrc 中,将 "requireConfigFile": false, 添加到 parserOptions 部分(在 "ecmaVersion": 8, 下方)(我需要这个或 babel 正在寻找我没有的配置文件)
    • 运行命令对文件进行 lint 处理

    然后,对于我来说只有你的两个配置文件,错误消失了,我得到了适当的 linting 错误。

    【讨论】:

    • 我也有同样的问题,在哪里可以找到.eslintrc文件?
    • 它通常与 package.json 在同一个文件夹中。但是,它可能在其他地方。从命令行可以指定它,或者 eslint 将在您正在 linting 的文件的文件夹或任何父文件夹中查找。如果您使用的是框架或 IDE 插件,它也可能在其他地方!它也不必称为 .eslintrc:there are several options for other names and formats,包括将配置放入 package.json 本身。简而言之,如果这更简单一点,将对每个人都有帮助。
    • 不幸的是,这对我不起作用。有没有人成功使用过这个解决方案?
    • 是的@DavidH - 它对我有用。尝试重新启动代码编辑器以反映更改,或尝试删除 node_modules 文件夹并重新安装 npm。它应该可以工作。
    【解决方案2】:

    “@babel/eslint-parser”依赖于“@babel/core”。所以在“npm install @babel/eslint-parser -S”之后,你应该npm i @babel/core -S

    【讨论】:

    • 之后呢?这不是答案。
    猜你喜欢
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2022-07-13
    • 2021-10-01
    • 2020-08-23
    • 2020-09-06
    • 2021-12-02
    相关资源
    最近更新 更多