Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vercel Error: ENOENT no such file or directory

I'm building a static blog using Nextjs 13 and deploying it on Vercel. I builded and started the project locally and everything was working, but on Vercel I got this error:

ERROR Error: ENOENT: no such file or directory, open 'posts/second.md' at Object.openSync (node:fs:600:3) at Object.readFileSync (node:fs:468:35) at getPostBySlug (/var/task/.next/server/chunks/207.js:146:63) at Post (/var/task/.next/server/app/posts/[slug]/page.js:603:52) at T (/var/task/.next/server/chunks/760.js:11441:25) at Ma (/var/task/.next/server/chunks/760.js:11604:33) at Array.toJSON (/var/task/.next/server/chunks/760.js:11397:32) at stringify () at V (/var/task/.next/server/chunks/760.js:11696:53) at ping (/var/task/.next/server/chunks/760.js:11496:43) { errno: -2, syscall: 'open', code: 'ENOENT', path: 'posts/second.md'}

The error happens when I go to the "/posts/second" route for example, not in the main page

This is the code interested:

const getPostBySlug = (slug: string) => {
const folder = "posts/";
const file = `${folder}${slug}.md`;
const content = fs.readFileSync(file, "utf8");
return matter(content)
};

The posts folder is located in the root folder.

I tried to modify the next config by adding the output option and setting it to 'standalone':

const nextConfig = {
   // config
   output: 'standalone',
}

I also tried to modify the path to locate the folder but nothing seems to work.

If more information is needed. project is published on GitHub

like image 483
Samuele1818 Avatar asked Sep 15 '25 05:09

Samuele1818


2 Answers

I solved my problem by looking at a tutorial provided by Vercel, so thanks to this line of code path.join(process.cwd(), 'posts'); the folder path is resolved.

like image 93
Samuele1818 Avatar answered Sep 16 '25 19:09

Samuele1818


(Not sure if this is the exact same problem!)

I solved it by removing the "api" folder from the pages directory.

  • Pre-build, and check if al the pages get build statically indicated by: '○' or '●' and non dynamic: 'λ'
  • In my case the app did not deploy on Vercel.
  • (Don't know why the error references a completely different file)

feel free to correct me if I'm wrong just hoping people don't go ghost hunting (like me) when it might be an easy fix =)

like image 26
Pintjens Avatar answered Sep 16 '25 17:09

Pintjens