1

I can run without problem npm run dev and the application works fine. But when I run npm run build I get the following error.

Build error occurred
Error: Build optimization failed: found pages without a React Component as default export in 
pages/category/[id]/getServerSideProps
pages/category/[id]/category

This is my folder structure:

enter image description here

index.ts:

import { CategoryPage } from './category';

export { getServerSideProps } from './getServerSideProps';
export default CategoryPage;

category.tsx:

import FinanceOptions from 'components/financeOptions';
import Head from 'next/head';
import React from 'react';
import { Container } from 'react-bootstrap';

interface CategoryPagePropsInterface {
    category: {
        id: number,
        name: string,
    }
}

export const CategoryPage = ({ category }: CategryPagePropsInterface): JSX.Element => (
  <div>
    <Head>
      <title>Category page</title>
      <meta name="description" content="Category page" />
      <link rel="icon" href="/favicon.ico" />
    </Head>

    <Container>
      <FinanceOptions category={category} />
    </Container>
  </div>
);

getServerSideProps:

import {requestCategoryById} from 'models/category/request';
import {NextPageContext} from 'next'

/**
 *
 * @param NextPageContext context
 */
export const getServerSideProps = async (context: NextPageContext) => {
    const {query: {handle}} = context;
    const { category } = await requestCategoryById(handle as string);
    return {props: {category}};
}
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Marco
  • 624
  • 1
  • 6
  • 21
  • 3
    Does this answer your question: [Can't build React/Next project - found page without a React Component as default export (context api file)](https://stackoverflow.com/a/65598867/1870780)? You should move both `category.tsx` and `getServerSideProps.ts` outside the `pages` folder. – juliomalves Aug 23 '21 at 22:07
  • 1
    Well yeah clearly that's what the error message said. I was kind of hopping there was a different solution than moving those files elsewhere. Thanks @juliomalves – Marco Aug 24 '21 at 15:11

0 Answers0