Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted import error: 'App' is not exported from './App'

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.

like image 431
Steve1101 Avatar asked Jul 03 '19 11:07

Steve1101


People also ask

Does not contain a default export imported as?

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 is named export?

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.

What is module exports in Reactjs?

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.


4 Answers

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.

like image 73
Michael Westcott Avatar answered Oct 19 '22 17:10

Michael Westcott


There are two ways to structure the import/export pair.

Default:

App.js

export default class App { ... }

index.js

import App from './App';

Named:

App.js

export class App { ... }

index.js

import { App } from './App';
like image 43
UncleDave Avatar answered Oct 19 '22 19:10

UncleDave


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!

like image 41
Santiago Ceron Avatar answered Oct 19 '22 19:10

Santiago Ceron


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.

like image 1
ravibagul91 Avatar answered Oct 19 '22 17:10

ravibagul91