How to access file from getServerSideProps() in Next.js?

Viewed 22

I have some Markdown files in my project that I wish to process within getServerSideProps(). I can access the filesystem when Next.js is running locally:

export async function getServerSideProps(context) {
    ...
    const mdDir = path.join(process.cwd(), 'src', 'md');
    const filepath = mdDir.join('example.md');
    content = await fs.promises.readFile(filepath, {encoding: 'utf8'});
    // perform processing on content based on SSR context
    ...
}

However, this doesn't work when running remotely in a server, you just get a Error: ENOENT: no such file or directory.

How do I do access file content from within getServerSideProps()? I don't necessarily know at build time which files will be needed at runtime, so I want to be able to access any files within an arbitrary source directory.

0 Answers
Related