React shows Attempted import error when importing a component or function
Attempted 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
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 buildConfirmed by react.dev
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.js2Fix
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 buildConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fixWeb apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fix