In my Next.js application (v14.2.2
) I want to read MDX files and show them as blogpost on my website. For this I am using the package next-mdx-remote
. Locally it works perfectly. However in production on a virtual host I get the error that a file already exists. This leads to an error 500
.
This is the exact error in the stderr.log
file:
⨯ Error: open EEXIST
at new Socket (node:net:428:13)
at process.getStdin [as stdin] (node:internal/bootstrap/switches/is_main_thread:224:17)
at get (<anonymous>)
at getOwn (node:internal/bootstrap/realm:206:5)
at BuiltinModule.syncExports (node:internal/bootstrap/realm:378:31)
at ModuleWrap.<anonymous> (node:internal/bootstrap/realm:358:17)
at BuiltinModule.getESMFacade (node:internal/bootstrap/realm:363:17)
at ModuleLoader.builtinStrategy (node:internal/modules/esm/translators:468:17)
at callTranslator (node:internal/modules/esm/loader:279:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:30) {
errno: -17,
code: 'EEXIST',
syscall: 'open'
}
This is my code in Next.js:
async function getPostByName(fileID: string): Promise<BlogPost | undefined> {
const fullPath = path.join(postsDirectory, `${fileID}.mdx`);
const rawMDX = fs.readFileSync(fullPath, 'utf8');
const { frontmatter, content } = await compileMDX<{
title: string, summary: string, date_created: string, date_updated: string, tags: string[], path: string }>({
source: rawMDX as MDXRemoteProps['source'],
components: { Video, CustomImage, CustomLink },
options: {
parseFrontmatter: true,
mdxOptions: {
format: 'mdx',
},
}
});
return { meta: { id: fileID, title: frontmatter.title, summary: frontmatter.summary, date_created: frontmatter.date_created, date_updated: frontmatter.date_updated, tags: frontmatter.tags, path: frontmatter.path }, content }
}
export default async function BlogPage() {
const fileNames = fs.readdirSync(postsDirectory);
const ids = fileNames.map((fileName) => {
return fileName.replace(/.mdx$/, '');
});
const posts: Meta[] = [];
for (const id of ids) {
const post = await getPostByName(id);
if (post) {
const {meta} = post;
posts.push(meta);
}
}
posts.sort((a, b) => (a.date_created < b.date_created ? 1 : -1));
// const posts = await getPostsMeta();
if (!posts) return <span>Something went wrong</span>
return (
<BlogPageClient posts={posts}/>
)
}
Do you know what could be the cause?