Next.js Discord

Discord Forum

Prerendering error on page even with 'use client'

Answered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
Hey
I have a problem in rendering my project
This is the full error :
Error occurred prerendering page "/dashboard/apply-seller". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: File is not defined
    at 14640 (/home/www-data/tobepaz-front/.next/server/app/(protected)/dashboard/apply-seller/page.js:1:87070)
    at Object.t [as require] (/home/www-data/tobepaz-front/.next/server/webpack-runtime.js:1:143)


The apply seller file doesn't contain anything related to that (I mean it's just importing a component)
import { MultiStepForm } from "../(components)/multi-step-form"

and inside MultiStepForm there isn't a file Neither, but we have :
import { DocumentUploadForm } from "./document-upload-form"
inside the multistep form and :
  const file = watch("nationalId")

  const validateFile = (file: unknown): string | null => {
    if (!(file instanceof File)) {
      return "فایل معتبری انتخاب نشده است";
    }
    if (file.size > 5 * 1024 * 1024) {
      return "حجم فایل باید کمتر از 5 مگابایت باشد";
    }
    if (!["image/jpeg", "image/png", "application/pdf"].includes(file.type)) {
      return "فرمت فایل باید JPG، PNG یا PDF باشد";
    }
    return null;
  };

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0]
    setFileError(null)

    if (file) {
      const error = validateFile(file)
      if (error) {
        setFileError(error)
        event.target.value = ""
        setValue("nationalId", undefined)
        setPreview(null)
        return
      }

      setValue("nationalId", file)
      if (file.type.startsWith("image/")) {
        const reader = new FileReader()
        reader.onloadend = () => {
          setPreview(reader.result as string)
        }
        reader.readAsDataURL(file)
      } else {
        setPreview(null)
      }
    } else {
      setValue("nationalId", undefined)
      setPreview(null)
    }
  }
Answered by Spectacled bear
Let's try basic things. Comment this code <MultiStepForm /> from /dashboard/apply-seller/page.js and see if you can build the project.
View full answer

30 Replies

Australian Freshwater CrocodileOP
I have "use client" on top of all the files
I'd appreciate if anyone could help me (AI and Docs didn't work)
Australian Freshwater CrocodileOP
Anyone?
Spectacled bear
Can you add some more details? If possible share the cpde for /dashboard/apply-seller/page.js
"use client"

import { MultiStepForm } from "../(components)/multi-step-form"


export default function Page() {
  return (
    <div className="">
        <MultiStepForm />
    </div>
  )
}
apply-seller/page.tsx looks like this
But i've used file inside document-upload-form.tsx imported in multistepform
Australian Freshwater CrocodileOP
Anyone?
Spectacled bear
Can you share one sandbox url? I know this is tedious, but I need to reproduce the error in order to help.
@Spectacled bear Can you share one sandbox url? I know this is tedious, but I need to reproduce the error in order to help.
Australian Freshwater CrocodileOP
I don’t think it would be able to do this
Because this part is like 1/500 of the project
And it depends on other parts
I can share anything if you want but remaking it would be near impossible
export const dynamic = "force-dynamic"
Spectacled bear
Let's try basic things. Comment this code <MultiStepForm /> from /dashboard/apply-seller/page.js and see if you can build the project.
Answer
Ok i've just noticed something
the problem is not the DocumentUploadForm
I commented it out and I got errors
Australian Freshwater CrocodileOP
I guess I've fixed it
I had a file validations.ts
The problem was that
It was trying to do the checks in that file and that was making it to be SSR which file instance should be dynamic
Thank you for your tip
Spectacled bear
Glad it fixed.