Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React shows Attempted import error when importing a component or function

ErrorAttempted import error:

Short answer

The build found the file but not the name you asked for. Either the import style does not match the export style (curly braces for named, none for default), or the name in the import differs from the exported name. Open the file you import from and make both sides match exactly.

  • Applies to React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the file named in the error and read its export lines. Compare them letter by letter with your import line.

    grep -n "export" src/components/Button.js
  2. 2Match what you see, then jump to its fix

    1. 1The file has export default, but your import uses braces, or it has export function and your import has none.Default export imported as named, or the other way roundFix
    2. 2The file exports several things and the name in your braces is not one of them: a typo, a renamed function, a deleted export.The imported name differs from the exported nameFix

What does the error look like?

  • Attempted import error:

Cause 1 of 2

Default export imported as named, or the other way round

1Check

Match the export line to the table: default is imported without braces and with any name, named is imported with braces.

grep -n "export" src/components/Button.js
grep -rn "components/Button" src/

2Fix

Change the import to the form the file uses, or change the file to the form you want and update every importer.

// Button.js has: export default function Button() {}
import Button from './Button.js';

// Button.js has: export function Button() {}
import { Button } from './Button.js';

3Verify

Restart the dev server or run the build. The error for that file should be gone.

npm run build

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

The imported name differs from the exported name

1Check

A named import must use exactly the same name on both sides. You can rename on import with as.

grep -n "export" src/store/index.js

2Fix

Correct the name, or rename on import if you want a different local name.

// store/index.js has: export const selectUser = ...
import { selectUser } from './store';
import { selectUser as getUser } from './store';

3Verify

Build again. The error for that name should be gone.

npm run build

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Pick one export style per project, for example named exports for everything but pages, and keep to it.
  • Let the editor add imports automatically, so names come from the file instead of memory.
  • Run the build in CI so a broken import fails before it ships.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.