22

Not sure why the error is coming up, heres my index.js and App.js (the default export). I have used export default in the app.js.

index.js:

import "./index.css";
import App from "./App";
import "bootstrap/dist/css/bootstrap.css";
import "font-awesome/css/font-awesome.css";
<App />;

App.js

import React, { Component } from "react";
import { ToastContainer } from "react-toastify";
import NavBar from "../components/navBar";
import auth from "../services/authService";
import "react-toastify/dist/ReactToastify.css";
import "./App.css";

class App extends Component {
  state = {};

  componentDidMount() {
    const user = auth.getCurrentUser();
    this.setState({ user });
  }

  render() {
    const { user } = this.state;

    return (
      <React.Fragment>
        <ToastContainer />
        <NavBar user={user} />
      </React.Fragment>
    );
  }
}

export default App;
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Keytoll
  • 469
  • 1
  • 5
  • 11

12 Answers12

30

Date: 23/01/2021

I've also faced this issue on my Next.js app.

If you're using a functional component instead of a class component you'll get this error as well in some casses. Exactly I don't know why this is happening but I just resolved this issue by exporting my component at the bottom of the page.

Like this,

Case Error scenario:

export default function Home(){
      return (<>some stuffs</>)
}

Home.getInitialProps = async (ctx) => {
    return {}
}
Error: The default export is not a React Component in page: "/"

How to resolved it?

I just put my export at the bottom my page and then it'll be gone.

Like this,

function Home(){
      return (<>some stuffs</>)
}

Home.getInitialProps = async (ctx) => {
    return {}
}

export default Home;

Hopefully, it'll be helpful for you!

Mohamed Jakkariya
  • 1,257
  • 1
  • 13
  • 16
13

In next.js all pages has to be exported default. Either this way

export default function RegisterPage() {
  return (
    <>
     
    </>
  );
}

or

const Home: NextPage = () => {
  return (
    <BaseLayout>
    
    </BaseLayout>
  );
};

export default Home;

If not you get that error.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
11

Fixed, made index.js a class and exported it.

import React, { Component } from "react";
import "./index.css";
import App from "./App";
import "bootstrap/dist/css/bootstrap.css";
import "font-awesome/css/font-awesome.css";
class Index extends Component {
  state = {};
  render() {
    return <App />;
  }
}

export default Index;
Keytoll
  • 469
  • 1
  • 5
  • 11
3

I was getting this error because of a completely different reason, In my case for some reason I removed the code In my app/layout.tsx and had forgotten about it, but this is waht was causing me to get the error error - Error: The default export is not a React Component in page: "/my_page".

To fix it I just put the following code in my app/layout.tsx file:

import './globals.css'

export const metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  )
}

This is probably a rare occurance but hopefully it helps.

  • 1
    Thank you! Your answer was helpful to me, because you drew my attention to the layout.tsx file. I created it, but forgot to write any code to it, so it was empty, hence at compilation, I was getting the error. – Arislan Makhmudov Aug 06 '23 at 04:54
1

This can also happen if you are throwing an error without catching it before the initial page renders during server-side-rendering. Take for instance the following:

const Component = (props) => {
  return (
    <div {...props}>
       hello!
    </div>
  );
};

Component.getInitialProps = async ({ query, req, asPath }) => {
  throw new Error('Application is crashing without catch');
}

Make sure you are catching unexpected errors prior to the page being rendered.

Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
1

Ran into this issue but in my case i was using the route group in next.js 13+. Check your layout.tsx / layout.jsx as well. Make sure both <your-component>.tsx and layout.tsx have default imports. E.g export default <your-component> in both files

hansss
  • 321
  • 4
  • 4
0

For me, this just happened because I had a .ts and not a .tsx file. It might do the same thing with .js instead of .jsx extensions.

jjhiggz
  • 49
  • 4
0

I experienced the same error: The default export is not a React Component in page: "/_app".

Next 12.2.5

In my case I was transitioning from a React app to Nextjs.

My index.js file was a functional component, but I was missing the following on my _app.js

import './App.css'

/* I was missing the following code */
function MyApp({ Component, pageProps }) {
    return <Component {...pageProps} />
}

export default MyApp
yaach
  • 386
  • 2
  • 3
0

This worked for me:

  1. Delete node_modules and yarn.lock/package-lock.json
  2. run yarn install or npm install
0

I had some problem with Next.js 13.4:

const Home = () => {
  return (
    <div>Home</div>
  )
}

export default Home

I solved them by just importing React:

import React from "react"

const Home = () => {
  return (
    <div>Home</div>
  )
}

export default Home
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Info All
  • 21
  • 1
  • 2
  • 6
0

I ran into this issue because I was using a fragment <></> around the html. After removing it, it worked as aspected.

aaronmbmorse
  • 359
  • 3
  • 10
0

I got the same issue in my next js app. The problem i was facing was because of the missing semi-colon at the end of export default page.

Previous code:

export default page

Fixed code:

export default page;
starball
  • 20,030
  • 7
  • 43
  • 238
Yasir Khan
  • 11
  • 2