Home › JavaScript

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined (JavaScript) — how to fix

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.

Check the render method of `App`.

Quick fix: Open the component named in 'Check the render method of X', and check each import used in its JSX. Match the import style to the export: export default Foo needs import Foo from './Foo'; export function Foo needs import { Foo } from './Foo'. Add console.log(Foo) before the return to find the undefined one.

What it means

For <Foo />, React needs Foo to be a string ("div") or a component (function or class). Here Foo was undefined (or an object), so React can't render it. The message tells you which parent to look at: Check the render method of App means one of the tags inside App's JSX is the bad one.

To find which import is broken, log them:

import Header from './Header';
import { Sidebar } from './Sidebar';

console.log({ Header, Sidebar });   // one of these prints undefined

Cause 1: default vs named import mismatch

The most common cause.

// Header.jsx
export function Header() { return <h1>Hi</h1>; }   // named export

// App.jsx
import Header from './Header';   // default import: undefined

Fix 1

Make the import match the export:

import { Header } from './Header';   // named export

or change the export:

export default function Header() { return <h1>Hi</h1>; }
// then: import Header from './Header';

The same applies to re-exports in index.js barrel files. export { default } from './Header' re-exports a default; export * from './Header' does not include the default export.

Cause 2: the component was never exported

The file defines the component but has no export, so any import of it is undefined. Bundlers sometimes only warn about this.

Fix 2

function Card() { /* ... */ }
export default Card;

Cause 3: the library version doesn't have that component

Imports from a package resolve to undefined when the name doesn't exist in the installed version. Well-known example: React Router v6 removed Switch (replaced by Routes) and the component prop.

import { Switch, Route } from 'react-router-dom';   // Switch is undefined in v6

Fix 3

Check the installed version and use that version's API:

npm ls react-router-dom
import { Routes, Route } from 'react-router-dom';

<Routes>
  <Route path="/" element={<Home />} />
</Routes>

Also check icon and UI libraries: a misspelled icon name (<FaHome /> vs a name that doesn't exist) gives the same error.

Cause 4: got: object

but got: object means you rendered a whole module or an object rather than the component in it:

import * as Button from './Button';
<Button />            // Button is the module namespace object

Fix 4

import Button from './Button';       // or import { Button } from './Button'

With CommonJS interop (e.g. a component published with module.exports = { default: Comp }), you may need .default. Mixing require() and export default causes the same problem: require('./Button') returns { default: Button }, so use require('./Button').default.

Cause 5: circular imports

If A.jsx imports B.jsx and B.jsx imports A.jsx (often through an index.js barrel), one of them may still be undefined at the moment it's used.

Fix 5

Import directly from the component file instead of the barrel, or move the shared code into a third module so the cycle disappears.

Last updated 2026-11-30.