[英]How to solve Eslint - Module.createRequire is not a function error?
I'm using react app with Parcel bundler & TypeScript.我正在使用带有 Parcel bundler 和 TypeScript 的 react 应用程序。 I'm trying to use ESLint but I get this error:
我正在尝试使用 ESLint,但出现此错误:
ESLint: Initialization error (ESLint). ESLint:初始化错误(ESLint)。 Module.createRequire is not a function.
Module.createRequire 不是 function。
versions:版本:
This is my setup:这是我的设置:
package.json: package.json:
{
"name": "app-boilerplate",
"version": "1.0.0",
"description": "",
"source": "public/index.html",
"scripts": {
"start": "parcel --port=3000 --open --lazy",
"build": "parcel build",
"lint": "eslint . --ext .tsx,.ts",
"lint:fix": "eslint . --ext .tsx,.ts --fix",
"test": "jest --coverage --passWithNoTests",
"test:watch": "jest --watchAll --coverage",
"prepare": "husky install",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.16.7",
"@parcel/transformer-svg-react": "^2.2.0",
"@storybook/addon-actions": "^6.4.13",
"@storybook/addon-essentials": "^6.4.13",
"@storybook/addon-links": "^6.4.13",
"@storybook/react": "^6.4.13",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@types/jest": "^27.4.0",
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"babel-loader": "^8.2.3",
"eslint": "^8.7.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-storybook": "^0.5.5",
"husky": "^7.0.4",
"jest": "^27.4.7",
"parcel": "^2.2.0",
"prettier": "^2.5.1",
"ts-jest": "^27.1.3",
"typescript": "^4.5.4"
},
"dependencies": {
"@types/node": "^17.0.8",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"alias": {
"@hooks": "./src/hooks",
"@libs": "./src/libs"
}
}
.eslintrc.json: .eslintrc.json:
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"airbnb",
"prettier",
"plugin:storybook/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint",
"jsx-a11y"
],
"rules": {
"import/extensions": [2, "never"],
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx", ".ts"] }],
"react/jsx-indent": [2, 2, {"checkAttributes": true, "indentLogicalExpressions": true}],
"react/react-in-jsx-scope": "off",
"no-console": "off"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
The actual error inside Webstorm Webstorm 内部的实际错误
How can I solve it, please?请问我该如何解决? Thank you.
谢谢你。
I had the same issue.我遇到过同样的问题。 Tried in Webstorm terminal following commands:
nvm current
(I use nvm) and node -v
.在 Webstorm 终端中尝试了以下命令:
nvm current
(我使用 nvm)和node -v
。 Both of these commands returned 16.14.0
.这两个命令都返回
16.14.0
。
But the issue only went away when I changed default used version of node in Webstorm editor itself.但是,当我在 Webstorm 编辑器本身中更改默认使用的节点版本时,问题才消失。 You can do this by pressing
CTRL + ALT + S
.您可以通过按
CTRL + ALT + S
来执行此操作。 Then select Languages & Frameworks
and after that Node.js
.然后是 select
Languages & Frameworks
,然后是Node.js
。 Value for Node interpreter:
was there 10 and I switched it there to 16.14.0. Node interpreter:
有 10 个,我把它切换到 16.14.0。 If you do not have this version od nodejs installed on your machine, I suggest you use nvm (node version manager).如果您的机器上没有安装此版本 od nodejs,我建议您使用 nvm(节点版本管理器)。
In my case the issue happened because I had created the project on WSL Ubuntu, so the solution was to (so adding on to Lazar's answer, in a way) change WebStorm's Node interpreter to WSL.在我的情况下,问题发生是因为我在 WSL Ubuntu 上创建了项目,所以解决方案是(因此在某种程度上添加了 Lazar 的答案)将 WebStorm 的节点解释器更改为 WSL。 Click on the
...
and then +
to add the Node/NPM installed on your WSL distros in case it doesn't show in the drop down list of interpreters.单击
...
,然后单击+
添加安装在 WSL 发行版上的 Node/NPM,以防它未显示在解释器的下拉列表中。
Simply updating your node version will resolve the issue.只需更新您的节点版本即可解决问题。 You can try Node 14 or 16 version.
您可以尝试 Node 14 或 16 版本。
Although you are saying using Node 16.6.2 but definitely it's not been used when you are running the script.尽管您说的是使用 Node 16.6.2,但在运行脚本时肯定没有使用它。
It is the problem of Node version, just update the Node to the correct version.是Node版本的问题,把Node更新到正确的版本就好了。https://eslint.org/docs/latest/user-guide/migrating-to-8.0.0 enter image description here
https://eslint.org/docs/latest/user-guide/migrating-to-8.0.0在此处输入图像描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.