Build issue with next.js project Even though local environment there are no errors

Hi,

I have been having some issues with deployment. it is for the custom site https://rejaurrahman.co.uk/. Previously I had the project running using HTML, sass, and javascript. I upgraded it using next.js and sanity.io CMS. I checked when running yarn build I am getting no errors plus when I run the project locally I am getting no errors.

This is the repository - https://github.com/RejaurRahman/portfolio-sanity

Don’t know why I am getting this issue.

Regards,

Ronny

@Ronny Your app link is private:

When you say:

when I run the project locally I am getting no errors

Do you mean your Development command or your Build command?

You should always confirm it works locally by running exactly the same command locally as you’re running on Netlify, since most systems do different things under the two modes and you can accidentally confuse yourself.

When I run your repository locally with npm run build I receive the following error:

image

The error itself points to help within the nextjs documentation:
https://nextjs.org/docs/messages/module-not-found

It looks like it’s case sensitivity related though, since it says it can’t resolve Headerlogo.module.scss while the file it’s within is HeaderLogo.component.tsx, notice the difference in capitalization for logo vs Logo.

I am getting another error now

I don’t get it because I have had the environment variables so I don’t know why its not working.

Regards,

Ronny

@Ronny Check how you’re constructing these URL’s as the error occurring is:

10:12:35 AM: TypeError: Failed to parse URL from
10:12:35 AM:       undefined/api/getContact

If the front portion for example is coming from the environment variables you mention, then make sure they’re set correctly in Netlify:
https://docs.netlify.com/configure-builds/environment-variables/

Hi, thank you for the reply. I added the environment variable on Netlify. I am getting this error. Not sure what might be causing this - Netlify App

Regards,

Ronny

@Ronny I can’t really help much with this one as I don’t really assist with Next.js based questions.

Projects built with it tend to have extra levels of complexity, automatically generated serverless functions, and use middleware, which all add up to extra potential points of failure.

That said, the root error is:

10:36:50 AM: SyntaxError: Unexpected token < in JSON at position 0
10:36:50 AM:     at JSON.parse (<anonymous>)

It indicates something is running JSON.parse and that the json is malformed.
The < is usually an indication that it has accidentally been given a HTML string instead of JSON string.

When Next.js fails it tends to provide a URL you can check in their documentation for possible solutions, you can see that here in your log:

10:36:50 AM: Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

Normally I might say to check your redirects to make sure you don’t have a catch all rule that’s inadvertently returning HTML for a route that’s supposed to be JSON, but with Next.js from my perspective it could be literally anything causing it.