12

Problem

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

I turn on esModuleInterop and allowSyntheticDefaultImports in tsconfig.json.
And I also use eslint-import-plugin and eslint-import-resolver-typescript for linting about import.

But, eslint says "No default export found in imported module "react"."

How to fix this?

Background

Tech Stack

  • react: 18.2.0
  • @types/react: 18.0.14
  • typescript: 4.7.4
  • eslint: 8.18
  • @typescript-eslint/eslint-plugin: 5.30
  • @typescript-eslint/parser: 5.30.5
  • eslint-import-resolver-typescript: 3.1.1
  • eslint-plugin-import: 2.26
  • eslint-plugin-react: 7.30 ...

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

{
  "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"
  }
}
3
  • 3
    No direct solution to your problem, but since React 18, importing react is not necessary. Commented Jul 10, 2022 at 14:40
  • 2
    Can you show us your package.json? Do you have @types/react and @types/react-dom installed as a devDependency? I was running into the same issue. Ensuring these were installed resolved my issue. Commented Sep 16, 2022 at 12:52
  • 1
    This answer explains why. Commented Feb 6, 2023 at 22:40

2 Answers 2

3

The * fixed my problem.

import * as React from 'react';
Sign up to request clarification or add additional context in comments.

Comments

-1

// tsconfig.json "allowSyntheticDefaultImports": true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.