Next.js Discord

Discord Forum

Readdir error (please help)

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
I am grabbing the course lesson metadata export inside of my files. This works perfectly fine inside of my dev environment and it loads all the UnitHeaders perfectly. Yet, inside of my production environment, it doesn't work. The error has to do with this line. If anyone can help, I'd appreciate it
        folders = await fs.readdir(`courses/${course}/lessons/`);

14 Replies

Brown bearOP
import UnitHeader from "@/components/UnitHeader/UnitHeader";
import { getCourseLessonsMetaData } from "@/utils/handleMetadata";

export default async function Article({
    params,
}: {
    params: { course: string };
}) {
    const coursesMetaData = await getCourseLessonsMetaData(params.course);
    console.log(coursesMetaData);

    return (
        <div className="flex flex-col items-center min-h-screen">
            {coursesMetaData && coursesMetaData.map((courseMetaData) => (
                <UnitHeader
                    course={params.course}
                    unitName={courseMetaData.unitName}
                    unitDescription={courseMetaData.unitDescription}
                    slug={courseMetaData.slug}
                />
            ))}
        </div>
    );
}
export async function getCourseLessonsMetaData(course: string) {
    let folders;
    try {
        folders = await fs.readdir(`courses/${course}/lessons/`);
    } catch (error) {
        console.error(`Error processing folder ${folders}:`, error);
        return null;
    }

    let courseLessonsMeta: CourseLessonMetaData[] = [];

    for (const folder of folders) {
        try {
            const module = await import(`/courses/${course}/lessons/${folder}/index.mdx`);
            const metadata = module.metadata;
            metadata.slug = folder;

            courseLessonsMeta.push(metadata);
        } catch (error) {
            // Handle the error here
            console.error(`Error processing folder ${folder}:`, error);
        }
    }

    courseLessonsMeta.sort((a,b) => (a.unitNumber < b.unitNumber ? 1 : -1));

    return courseLessonsMeta;
}
Brown bearOP
I've been working on this for 10 hours
I'm not great at coding
So I'd appreciate any help
Toyger
what error?
Brown bearOP
The left is the error on my website
The right is the vercel log of the error
This is how the website should look (as it does on my dev server)
@Toyger what error?
Brown bearOP
also thank you for helping!
@Brown bear also thank you for helping!
Toyger
because ephermal storage of vercel it only possible if you put your data somewhere in app folder
https://vercel.com/guides/loading-static-file-nextjs-api-route
also you can see here that vercel using fs.promises maybe that is also mandatory, not sure about that.
Brown bearOP
I will take a look into that
Thanl you