简体   繁体   English

ESLint 在导入的模块“react”中找不到默认导出

[英]ESLint No default export found in imported module "react"

Problem问题

import React, { useContext, useEffect, useRef } from 'react';

I turn on esModuleInterop and allowSyntheticDefaultImports in tsconfig.json .我在tsconfig.json中打开esModuleInteropallowSyntheticDefaultImports
And I also use eslint-import-plugin and eslint-import-resolver-typescript for linting about import.我还使用eslint-import-plugineslint-import-resolver-typescript

But, eslint says " No default export found in imported module "react". "但是,eslint 说“ No default export found in imported module "react".

How to fix this?如何解决这个问题?

Background背景

Tech Stack技术栈

  • react: 18.2.0反应:18.2.0
  • @types/react: 18.0.14 @types/反应:18.0.14
  • typescript: 4.7.4打字稿:4.7.4
  • eslint: 8.18埃斯林特:8.18
  • @typescript-eslint/eslint-plugin: 5.30 @typescript-eslint/eslint-plugin:5.30
  • @typescript-eslint/parser: 5.30.5 @typescript-eslint/解析器:5.30.5
  • eslint-import-resolver-typescript: 3.1.1 eslint-import-resolver-typescript:3.1.1
  • eslint-plugin-import: 2.26 eslint 插件导入:2.26
  • eslint-plugin-react: 7.30 ... eslint 插件反应:7.30 ...

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "isolatedModules": true,
    "noEmit": true,
    "resolveJsonModule": true,
    "jsx": "react-jsx",
    "jsxImportSource": "@emotion/react",
    "baseUrl": "./src",
    "paths": {
      "@components/*": ["components/*"],
      "@styles/*": ["styles/*"],
      "@custom-types/*": ["custom-types/*"],
      "@pages/*": ["pages/*"],
      "@assets/*": ["assets/*"],
      "@constants": ["constants.ts"],
      "@api/*": ["api/*"],
      "@context/*": ["context/*"]
    },
    "typeRoots": ["src/custom-types"]
  },
  "include": ["src"]
}

.eslintrc.json .eslintrc.json

{
  "env": {
    "browser": true,
    "es2022": true
  },
  "settings": {
    "react": {
      "version": "detect"
    },
    "import/resolver": {
      "typescript": {
        "project": "frontend/tsconfig.json"
      }
    }
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "extends": [
    "plugin:jsx-a11y/recommended",
    "plugin:react/recommended",
    "plugin:react/jsx-runtime",
    "plugin:react-hooks/recommended",
    "plugin:import/recommended",
    "plugin:import/typescript",
    "plugin:@typescript-eslint/recommended",
    "plugin:storybook/recommended",
    "plugin:prettier/recommended"
  ],
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never",
        "tsx": "never",
        "js": "never",
        "jsx": "never"
      }
    ],
    "react/prop-types": "off",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-unused-vars": "off",
    "@typescript-eslint/no-empty-function": "off",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/no-empty-interface": "off"
  }
}

*解决了我的问题。

import * as React from 'react';

暂无
暂无

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

相关问题 React Component ESLint表示“首选默认导出” - React Component ESLint says Prefer Default Export 在“三”中找不到导出“默认”(导入为“三”) - export 'default' (imported as 'Three') was not found in 'three' 在“@mui/lab/LoadingButton”中找不到导出“默认”(导入为“LoadingButton”)(模块没有导出) - export 'default' (imported as 'LoadingButton') was not found in '@mui/lab/LoadingButton' (module has no exports) 在“@fortawesome/react-fontawesome”中找不到导出“默认”(导入为“FontAwesomeIcon”)(可能的导出:FontAwesomeIcon) - export 'default' (imported as 'FontAwesomeIcon') was not found in '@fortawesome/react-fontawesome' (possible exports: FontAwesomeIcon) 在“@coreui/react”中找不到导出“CDataTable”(导入为“CDataTable”) - export 'CDataTable' (imported as 'CDataTable') was not found in '@coreui/react' 默认导出 eslint 问题 - default export eslint issues 在“react”中找不到导出“unstable_act”(导入为“React”) - Export 'unstable_act' (imported as 'React') was not found in 'react' 在“../x/x”中找不到导出“默认”(导入为“x”)(可能的导出:x) - export 'default' (imported as 'x') was not found in '../x/x' (possible exports: x) 我该如何解决这个错误? “在'./App'中找不到导出'默认'(导入为'App')(可能的导出:App)”(我使用反应) - How can I solve this error? "export 'default' (imported as 'App') was not found in './App' (possible exports: App)" (im using react) 在“./theme”中找不到导出“主题”(作为“主题”导入)(可能的导出:默认) - export 'theme' (imported as 'theme') was not found in './theme' (possible exports: default)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM