我当前正在使用React、TypeScript、样式化组件、Webpack等设置样板文件,但在尝试运行ESLint时出现错误:
错误:必须使用导入来加载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
,而我不是。
8条答案
按热度按时间fhg3lkii1#
我认为问题是你正在尝试使用过时的babel-eslint parser,上次更新是在一年前,看起来它不支持ES6模块。更新到latest parser似乎是可行的,至少对于简单的linting来说是这样。
因此,请执行以下操作:
"babel-eslint": "^10.0.2",
行更新为"@babel/eslint-parser": "^7.5.4",
。这适用于上面的代码,但最好使用最新版本,在编写时是7.16.3。npm i
"parser": "babel-eslint",
更新为"parser": "@babel/eslint-parser",
"requireConfigFile": false,
添加到parserOptions部分(在"ecmaVersion": 8,
下面)(我需要这个,或者babel正在寻找我没有的配置文件)然后,对于我来说,只有你的两个配置文件,错误消失,我得到适当的棉绒错误。
mwyxok5s2#
这可能发生在很多最近发布的框架中,比如Vue.js或Nuxt.js,主要是因为使用了ESM模块(旧的Node.js版本不完全或根本不支持)。
最快的方法是使用类似nvm的代码,安装后,您可以运行:
nvm i 16
(v16是截至编写时的最新LTS)nvm use 16
您将在完全支持ESM的情况下运行该项目,您可以通过运行
node -v
再次检查。vmpqdwk33#
通过将.eslintrc的文件扩展名从js更改为json可以轻松解决此问题(使用ESM格式导出.eslintrc对象到.eslintrc.js中是不起作用的)。
pokxtpni4#
在Visual Studio Code中,删除ESLint扩展,然后重新安装。
dldeef675#
对于使用 *eslint-config-react-native-community软件包 * 的“React Native”设置
我按照accepted answer的建议做了,但是我在根文件夹的 package.json 文件中没有任何
"babel-eslint": "^10.0.2",
行。node_modules/@react-native-community/eslint-config
"babel-eslint": "^10.0.2",
更改为"@babel/eslint-parser": "^7.18.2",
"parser": "babel-eslint",
更改为parser: '@babel/eslint-parser',
npm i -D @babel/eslint-parser
这对我很有效。
vmdwslir6#
我听从了@rich-n上面一条评论的建议,根据这条评论:
转到
.eslintrc.js
并确保您具有以下设置:这样做会从vscode中删除所有“红色”标记:)
oxcyiej77#
如果执行上述所有操作后仍然失败,请重新加载vscode。
oipij1gg8#
“@babel/eslint-parser”依赖于“@babel/core”。因此,在“npm install @babel/eslint-parser -S”之后,您应该使用
npm i @babel/core -S
。