Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

React: Element type is invalid, got undefined

React received undefined where JSX needs a component: a default/named import mismatch, a circular barrel, or lazy without a default export.

~5 min readReact
#React#JSX#Modules#Debugging
A React component crate with an empty slot, a cyan import gate and an amber default export tag beside it

Introduction & Quick Summary

text
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

React received undefined where JSX expects a component. In production builds the same crash is Minified React error #130, with no hint at all, which is why this error is worth understanding once.

The import is almost always the cause, not the component:

  • Default vs named mismatch — import Button from "./Button" against export function Button, or the reverse.
  • The file exports nothing — a missing export default, a file that only defines a type, or a re-export barrel that loses the name.
  • A circular import — the module evaluates before its export exists, so the binding is undefined at first render.
  • React.lazy pointed at a module without a default export — lazy reads module.default.
  • Two React copies or a version jump — a library component created by a different React instance, or an export removed in a major version.
SymptomMost likely causeFirst check
Works in one file, fails after movingDefault/named importThe exporting file's export keyword
Fails only on first render, then worksCircular importImport graph around the barrel file
Fails only for one library componentVersion mismatch / renamed exportnpm ls react react-dom
Fails only in productionMinified #130Reproduce in dev build
Fails after adding a barrel index.tsRe-export missing or circularBarrel contents and order
Fails inside lazy boundaryNo default exportAdd export default or map it

Core Mechanics, Signatures & Reproducible Setup

JSX is a function call. <Foo /> compiles to jsx(Foo, {}), and React checks what Foo is at render time: a string for host elements ("div"), a function or class for composite components, or the special types Fragment, Suspense, Profiler. Anything else — undefined, null, a plain object, a number — triggers this error.

A failed import is the usual path to undefined. ES module semantics matter here: a missing named export is a build-time error in strict bundlers, while a missing default import silently yields undefined, which is exactly what React reports.

Reproduce the two most common shapes:

tsx
// Button.tsx export function Button({ label }: { label: string }) { return <button>{label}</button>; }
tsx
// App.tsx import Button from "./Button"; // undefined: no default export export default function App() { return <Button label="Save" />; // Element type is invalid }
tsx
// icons.ts export * from "./icons/arrow"; export * from "./icons/close";
tsx
// palette.tsx import { Arrow } from "./icons"; // barrel also imports palette export function Palette() { return <Arrow />; // may be undefined during the cycle }

The build passes in both cases with most bundlers, so the failure is a runtime render, not a compile error. That is the trap: type checking often only sees any from a loose declaration or does not catch the default import at all when esModuleInterop is on.

Key Comparison Table

Import styleRequires in exporterValue if mismatched
import X from "./x"export default Xundefined
import { X } from "./x"export function X / export const XBuild error in strict ESM
import * as X from "./x"Any named exportNamespace object
const X = lazy(() => import("./x"))export defaultundefined on resolve
lazy(() => import("./x").then(m => ({ default: m.X })))Named exportWorks
DiagnosisCommand or checkConfirms
Export shapeOpen the exporting file's export linesDefault vs named
Duplicate Reactnpm ls react react-domTwo versions in the tree
Circular importBundler's circular-dependency warningBarrel cycle
Lazy targetLog the resolved moduleMissing default
Production-onlynext build then next start locallyMinified #130

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Default and named, fixed at the boundary

tsx
// broken import Button from "./Button";
tsx
// fixed — match the exporter import { Button } from "./Button";

If you cannot change every call site, add the default re-export in the component file:

tsx
export function Button(props: ButtonProps) { /* ... */ } export default Button; // now both import styles work

Prefer one style per project. Mixed conventions are how this error keeps coming back after a refactor.

Case 2: lazy needs a default export

tsx
const Chart = lazy(() => import("./Chart")); // Chart.tsx exports named only

lazy resolves the module and reads default. Map the named export instead:

tsx
const Chart = lazy(() => import("./Chart").then((m) => ({ default: m.Chart })) );

Wrap it in Suspense with a fallback, otherwise the boundary has nothing to render while the chunk loads and you get a different warning.

tsx
<Suspense fallback={<p>Loading chart…</p>}> <Chart data={data} /> </Suspense>

Case 3: A barrel file that creates a cycle

ts
// components/index.ts export * from "./Palette"; export * from "./icons/Arrow";
tsx
// components/Palette.tsx import { Arrow } from "./index"; // cycle: index -> Palette -> index

During evaluation Palette loads index, which has not finished defining Arrow yet, so the binding is undefined at module scope. Import from the concrete module inside the barrel's own subtree:

tsx
import { Arrow } from "./icons/Arrow";

If a component only needs the type, use import type so the import disappears at compile time and cannot create a runtime cycle.

Case 4: Two React copies

bash
npm ls react react-dom # ├─ react@19.2.0 # └─ some-lib@1.4.0 # └─ react@18.3.1 deduped? no — nested

A component exported by a library built against React 18 can still render, but its internal hooks bind to a different dispatcher and behavior breaks in ways that look unrelated. Dedupe with an override:

{ "overrides": { "react": "$react", "react-dom": "$react-dom" } }

For a workspace monorepo, also check that the package is not hoisted twice by pnpm with different peer resolutions.

Case 5: A component that is an object, not a function

tsx
const Card = { Header, Body }; // plain object return <Card />; // Element type is invalid

Object namespaces are not components. Destructure the piece you need, or wrap the object in a real component:

tsx
function CardShell({ children }: { children: React.ReactNode }) { return <section className="card">{children}</section>; }

The same happens with const Field = { Text: TextField, Select } from generated form libraries; use Field.Text, not Field.

Case 6: A library's export was removed or renamed

tsx
import { Link } from "react-router"; // v7: package is react-router import { Link } from "react-router-dom";

Major upgrades change entry points (react-router-dom to react-router in v7, @mui/material subpath moves, icon packages reorganized). When one component fails and every other import in the file works, check that library's changelog before touching your own code.

bash
npm view react-router version peerDependencies

Common Compilation/Runtime Mistakes & Version Gotchas

  • Trusting the type checker here. A default import from a CommonJS module with esModuleInterop compiles even when the runtime value is undefined; the error only appears at render.
  • Assuming export * re-exports defaults. It does not. export { default } from "./x" is required to pass a default through a barrel.
  • Ignoring the production-only case. next build + next start locally reproduces #130 with the same stack; a dev-only fix session leaves it for users.
  • React 19 and forwardRef. Components wrapped in forwardRef still work, but ref is now a regular prop; a library that dropped the wrapper can return a different export shape.
  • Fast Refresh masking the cycle. A broken circular import often fixes itself on hot update and returns after a full reload; verify with a fresh server start.
  • Case-insensitive filesystems. import "./button" resolving to Button.tsx on macOS can fail as undefined on Linux CI; keep file and import casing identical.

To compare two dependency trees after an upgrade, paste both npm ls --json outputs into the JSON formatter and diff them with the diff tool. Both make a nested duplicate obvious in seconds.

Primary references: lazy documents the default-export requirement for lazy-loaded components, and Importing and Exporting Components covers the default/named rules that produce this error.

Official references

primary documentation

The React documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit React work — they run entirely in this browser tab, so nothing you paste leaves your device.

All React guides

More in React

All React guides

The guides filed next to this one in React, in library order.