Home does not contain an export named Home
Asked Answered
P

10

145

I was working with create-react-app and came across this issue where I get an error:

Home does not contain an export named Home.

Here's how I set up my App.js file:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { Home } from './layouts/Home'

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

export default App;

Now in my layouts folder I have the Home.js file, which is setup like following:

import React, { Component } from 'react';

class Home extends Component {
  render() {
    return (
      <p className="App-intro">
        Hello Man
      </p>
    )
  }
}

export default Home;

As you can see, I am exporting the Home component. But I get an error in my console saying this:

enter image description here

What is going on?

Pondicherry answered 25/5, 2017 at 5:12 Comment(0)
A
316

The error is telling you that you are importing incorrectly. Here's the code you have to add:

import { Home } from './layouts/Home';

This is incorrect because you're exporting as the default export, not as a named export. Check this line:

export default Home;

You're exporting as default, not as a name. Thus, import Home like this:

import Home from './layouts/Home';

Notice there are no curly brackets. Further reading on import and export.

Ahvenanmaa answered 25/5, 2017 at 5:14 Comment(7)
Or you could also do a named export. Ex. export {Home};Batten
@AbhinavSingi Yes, but it's convention and widely practiced to export a component as the default export of a module. Plus there are no other exports.Ahvenanmaa
Yes, exactly @AndrewLi, we also follow the same practice :)Batten
Awesome so multiple would be wrapped in curly brackets compared with singular as seen here.Aromatic
@Aromatic Yes, if you have multiple exports, use named ones. Then import using that name as seen in the linked MDN documentation.Ahvenanmaa
Oh man, thanks for this! It was driving me nuts trying to figure out what I'd done wrong. Talk about a persnckety programming language. Yikes. Is there a vs plugin that will automatically format js/tsx code to make it harder to make these mistakes?Alphonse
I was able to fixed the warnings messages using import * as Dash from 'react-native-dash' from webpack. I suspect some aspect of this implementation has been deprecated.Rienzi
M
14

Use

import Home from './layouts/Home'

rather than

import { Home } from './layouts/Home'

Remove {} from Home

Mercurate answered 24/8, 2017 at 12:34 Comment(1)
What else does this add to the existing answer?Ahvenanmaa
S
4

This is a case where you mixed up default exports and named exports.

When dealing with the named exports, if you try to import them you should use curly braces as below,

import { Home } from './layouts/Home'; // if the Home is a named export

In your case the Home was exported as a default one. This is the one that will get imported from the module, when you don’t specify a certain name of a certain piece of code. When you import, and omit the curly braces, it will look for the default export in the module you’re importing from. So your import should be,

import Home from './layouts/Home'; // if the Home is a default export

Some references to look :

Stuckup answered 24/9, 2018 at 17:40 Comment(0)
Q
1

I just ran into this error message (after upgrading to nextjs 9 some transpiled imports started giving this error). I managed to fix them using syntax like this:

import * as Home from './layouts/Home';
Quarantine answered 23/7, 2019 at 8:46 Comment(0)
B
1

We also can use

import { Home } from './layouts/Home'; 

using export keyword before class keyword.

export class Home extends React.Component{
    render(){
        ........
    }
}

For default

 import Home from './layouts/Home'; 

Default export class

 export default class Home extends React.Component{
    render(){
        ........
    }
 }

Both case don't need to write

export default Home;

after class.

Berkowitz answered 10/2, 2020 at 10:42 Comment(0)
C
0

put export { Home }; at the end of the Home.js file

Caresse answered 9/12, 2020 at 15:23 Comment(1)
in the question it state that he did ?Extroversion
B
0

Crazy as it may sound, i spent almost an hour to resolve a similar issue, restarted the localhost and it picked up by itself. Ridiculous but something restart fixes everything. For me component in question was logout.

I imported like : import {Logout} from './components';

with this define dint he Logout component export default Logout

Began answered 4/10, 2022 at 9:1 Comment(0)
A
0

If you're using [email protected] it isn't your fault the build is just bugged. Reverting to [email protected] will fix your problem

Appose answered 24/1 at 0:0 Comment(0)
H
-1

You can use two ways to resolve this problem, first way that i think it as best way is replace importing segment of your code with bellow one:

import Home from './layouts/Home'

or export your component without default which is called named export like this

import React, { Component } from 'react';

class Home extends Component{
    render(){
        return(
        <p className="App-intro">
          Hello Man
        </p>
        )
    }
} 

export {Home};
Hereby answered 14/6, 2019 at 11:52 Comment(0)
P
-1

This is the solution:

  • Go to your file Home.js
  • Make sure you export your file like this in the end of the file:
export default Home;
Pillory answered 20/7, 2020 at 10:53 Comment(1)
This doesn't seem like a good answer and should probably be downvoted. Please read How to answer before you post another anwer. Generally, you also shouldn't answer old questions with many other answers - unless you can add something new the other answers can't. Also include code.Selfcommand

© 2022 - 2024 — McMap. All rights reserved.