简体   繁体   English

如何解决 Eslint - Module.createRequire 不是 function 错误?

[英]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:版本:

  • IDE: Webstorm IDE:网络风暴
  • Node Version: 16.6.2节点版本:16.6.2
  • NPM Version: 7.20.3 NPM 版本:7.20.3
  • I don't have ESLint installed globally in my machine我的机器上没有全局安装 ESLint

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 herehttps://eslint.org/docs/latest/user-guide/migrating-to-8.0.0在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 eslint 中的“预期在箭头函数中返回值”错误 - How to solve "Expected to return a value in arrow function" error in eslint 如何通过es6导入导入nodejs模块“module”以使用“createRequire”创建“require”函数以使用节点的“require”? - How to import nodejs module "module" via es6 import to create "require" function with "createRequire" in order to use node's "require"? 如何解决这个错误 JSONEditor is not defined ESlint - How to solve this error JSONEditor is not defined ESlint 如何解决组件应该写成eslint-react中的纯函数错误? - how to solve Component should be written as a pure function error in eslint-react? 如何将 for of 循环转换为 for 循环以解决 ESLint 错误 - How to convert for of loop into a for loop to solve the ESLint error 如何解决,craco: *** Cannot find ESLint loader (eslint-loader)。 *** ANTd 和 React 错误 (2021) - How to solve, craco: *** Cannot find ESLint loader (eslint-loader). *** error with ANTd and React (2021) 如何使用 Flow 解决无类型模块错误? - How to solve Untyped module error with Flow? 如何使用谷歌地图解决'no-unused-var'的ESLint错误 - how can i solve ESLint error that 'no-unused-var' with google map 在不禁用 eslint 的情况下解决未使用的表达式错误? - Solve no unused Expression Error with out disabling eslint? 如何解决节点模块的脚本错误? - How to solve Script error for: node module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM