I am trying to deploy my nextJS project however the console keeps returning this error
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:6498:19)
at successSteps (node:internal/deps/undici/undici:6472:27)
at node:internal/deps/undici/undici:1145:60
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:204:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
info - Generating static pages (4/4)
> Build error occurred
Error: Export encountered errors on following paths:
/
at /vercel/path0/node_modules/next/dist/export/index.js:425:19
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/trace/trace.js:79:20)
at async /vercel/path0/node_modules/next/dist/build/index.js:1422:21
at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/trace/trace.js:79:20)
at async /vercel/path0/node_modules/next/dist/build/index.js:1280:17
at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/trace/trace.js:79:20)
at async Object.build [as default] (/vercel/path0/node_modules/next/dist/build/index.js:73:29)
Error: Command "npm run build" exited with 1
BUILD_UTILS_SPAWN_1: Command "npm run build" exited with 1
When I try to build it locally on my terminal I also get a very similar error (using "npm run build")
Error: Export encountered errors on following paths:
/
at /Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/export/index.js:425:19
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Span.traceAsyncFn (/Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/trace/trace.js:79:20)
at async /Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/build/index.js:1422:21
at async Span.traceAsyncFn (/Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/trace/trace.js:79:20)
at async /Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/build/index.js:1280:17
at async Span.traceAsyncFn (/Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/trace/trace.js:79:20)
at async Object.build [as default] (/Users/username/Desktop/Projects/React/Next/typescript-portfolio/node_modules/next/dist/build/index.js:73:29)
I've tried looking at NextJS prerender error page but I haven't gotten any luck with the tips through there. I'm still pretty new to Next and decided to learn through it.
I am using a "getStaticProps" but I'm not sure if thats what the problem is. This is my code in index.tsx
export const getStaticProps: GetStaticProps<Props> = async () => {
const pageInfo = await fetchPageInfo();
const experiences = await fetchExperiences();
const projects = await fetchProjects();
const skills = await fetchSkills();
const socials = await fetchSocials();
return {
props: {
pageInfo,
experiences,
projects,
skills,
socials,
},
// Next.js will attempt to re-generate the page:
// - When a request comes in
// - At most once every day
revalidate: 86400, // In seconds
};
};
Looking at the error it doesn't say a specific page is erroring out, only "/" which leads me to believe it is a problem in my index file.
Here is an example of my code to fetch the api request in utils/fetchProject.ts
import { Project } from "@/typings";
export const fetchProjects = async () => {
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getProject`);
const data = await res.json();
const projects: Project[] = data.projects;
return projects;
};
Here is an example of my code to get the api response in pages/api/getProject.ts
import { sanityClient } from "../../sanity";
import { Project } from "../../typings";
const query = groq`
*[_type == "project" ] {
...,
technologies[]->
}
`;
type Data = {
projects: Project[];
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
const projects: Project[] = await sanityClient.fetch(query);
res.status(200).json({ projects });
}
I've been following this youtube video as a walkthrough.