2

So I decided to learn React and on the very first project I got an error message.

Failed to compile

./src/index.js

Attempted import error: './App' does not contain a default export (imported as 'App').

It's simple "Hello World" code. I have 2 files for that.

App.js

import React from 'react';
import ReactDOM from 'react-dom';
import HelloWorld from './HelloWorld';

ReactDOM.render(
  <HelloWorld />,
  document.getElementById('root')
);

HelloWorld.js

import React, { Component } from 'react';
 
class HelloWorld extends Component {
  render() {
    return (
      <div className="helloContainer">
        <h1>Hello, world!</h1>
      </div>
    );
  }
}
 
export default HelloWorld;

I read every post about this problem, none of them works. I also read that it's usually problem with reactstrap, but I don't have it installed.

Any help would be appreciated.

1
  • You need to show us your ./src/index.js file code since that is where the error is saying the problem is. Commented Feb 2, 2019 at 16:00

2 Answers 2

3

Your compiler is telling you that you're not exporting anything from App.js, and that this is an error within your index.js (which you haven't shown code for, but that's where you would typically call ReactDOM.render).

Your App.js should look more like your HelloWorld.js (an exported component), and you should be calling ReactDOM.render inside of index.js. This is the only time you'll call ReactDOM.render.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

App.js

import React, { Component } from 'react';
import HelloWorld from './HelloWorld';

class App extends Component {
  render() {
    return (
      <div className="App">
        <HelloWorld />
      </div>
    );
  }
}

export default App;

HelloWorld.js

import React, { Component } from 'react';

class HelloWorld extends Component {
  render() {
    return (
      <div className="helloContainer">
        <h1>Hello, world!</h1>
      </div>
    );
  }
}

export default HelloWorld;
Sign up to request clarification or add additional context in comments.

Comments

1

Rename the App.js file to index.js and delete any other index files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.