3

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

Samuele1818
  • 336
  • 4
  • 11
  • I tested this on my end, and it works perfectly fine. I would suggest restarting the server since these types of "random" errors often occur, and the fix is just to restart the server. – undevable Dec 26 '22 at 21:36
  • I deleted and recreated the project on Vercel but it's not working, I forgot to specify that the error happens when I go to the "/posts/second" route for example, not in the main page. – Samuele1818 Dec 26 '22 at 23:01

1 Answers1

6

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.

Samuele1818
  • 336
  • 4
  • 11
  • What is `process.cwd()` with respect to path of `posts`? I'm having a similar issue I currently have files inside the following folder, and it did not work: `path.join(process.cwd(),\`/pages/api/email/email-templates/${templateNameTxt}\`),` – guest Dec 31 '22 at 05:53
  • I think you have to move the `email-templates` folder into the root folder (so the path of the dir will be `/email-templates`), and then change the code to `path.join(process.cwd(), 'email-templates/)`. Now you have access to the folder and you can read the files that it contains using the `fs`. – Samuele1818 Jan 03 '23 at 00:02
  • I've been suffering from this bug for 3+ days, just a note for those who are going through this issue: Vercel only supports Yarn 1, so anything 2+ is going to have this issue. I have pretty much read every blog and documentation for the past 3 days and have not solved this problem. https://vercel.com/guides/does-vercel-support-yarn-2 discovered this issue via this StackOverflow thread: https://stackoverflow.com/questions/74529208/file-path-in-nextjs-api-route-not-resolving – guest Jan 03 '23 at 00:29
  • Also, you cannot have variable in your template names, ie. `/pages/api/email/email-templates/${templateNameTxt}` will not be accepted. Only the exact template file name will be recognized. this is because the template file variable is not found at build time and Vercel throws an error. – guest Mar 13 '23 at 20:27