I'm working on a currency converter app, I got the code from code pen (https://codepen.io/jmean/pen/Oxmgxp). I'm trying to run this code and I get this error: ./src/index.js Attempted import error: 'App' is not exported from './App'.
I've tried adding export default App(); on index.js and also on App.js file but its not working.
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import {App} from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
I expected the code to compile and display on the browser like it does on code pen.
The "does not contain a default export" error occurs when we try to use a default import to import from a module that doesn't have a default export. To solve the error, make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule .
What are Named Exports? Named exports allow us to share multiple objects, functions or variables from a single file and were introduced with the release of ES2015. Named exports are imported with curly braces in various files and must be imported using the name of the object, function or variable that was exported.
The export statement is used in Javascript modules to export functions, objects, or primitive values from one module so that they can be used in other programs (using the 'import' statement). A module encapsulates related code into a single unit of code.
If it's a default export, you don't need to put the import in curly braces.
import App from './App';
is what you should write.
There are two ways to structure the import/export pair.
App.js
export default class App { ... }
index.js
import App from './App';
App.js
export class App { ... }
index.js
import { App } from './App';
I had an error importing react on App component, it was a fool problem i was importing in a bad way the react core.
this compiled but had that error
import {ComponentName} from "./ComponentName"
this worked:
import ComponentName from './ComponentName'
it was strange, but hope helps!
There are two ways to export,
Named Export
With named exports, one can have multiple named exports per file. Then import the specific exports they want surrounded in braces. The name of imported module has to be the same as the name of the exported module.
// imports
import { MyComponent } from "./MyComponent";
// exports from ./MyComponent.js file
export const MyComponent = () => {}
Default Export
One can have only one default export per file. When we import we have to specify a name and import like:
// import
import MyDefaultComponent from "./MyDefaultExport";
// export
const MyComponent = () => {}
export default MyComponent;
Note: The naming of import is completely independent in default export and we can use any name we like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With