简体   繁体   English

Nextjs 13 Vercel 错误:ENOENT 没有这样的文件或目录

[英]Nextjs 13 Vercel Error: ENOENT no such file or directory

I'm building a static blog using Nextjs 13 and deploying it on Vercel.我正在使用 Nextjs 13 构建一个 static 博客并将其部署在 Vercel 上。 I builded and started the project locally and everything was working, but on Vercel I got this error:我在本地构建并启动了该项目,一切正常,但在 Vercel 上出现此错误:

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'}错误错误:ENOENT:没有这样的文件或目录,在 Object.openSync (node:fs:600:3) 在 Object.readFileSync (node:fs:468:35) 在 getPostBySlug (/var /task/.next/server/chunks/207.js:146:63) 在 Post (/var/task/.next/server/app/posts/[slug]/page.js:603:52) 在 T ( /var/task/.next/server/chunks/760.js:11441:25) 在 Ma (/var/task/.next/server/chunks/760.js:11604:33) 在 Array.toJSON (/var /task/.next/server/chunks/760.js:11397:32) 在 stringify () 在 V (/var/task/.next/server/chunks/760.js:11696:53) 在 ping (/var /task/.next/server/chunks/760.js:11496:43) { 错误号:-2,系统调用:'open',代码:'ENOENT',路径:'posts/second.md'}

The error happens when I go to the "/posts/second" route for example, not in the main page例如,当我 go 到“/posts/second”路由时,错误发生在主页上

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' :我尝试通过添加output选项并将其设置为“独立”来修改下一个配置:

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项目发布于GitHub

I solved my problem by looking at a tutorial provided by Vercel , so thanks to this line of code path.join(process.cwd(), 'posts');我通过查看Vercel 提供的教程解决了我的问题,所以感谢这行代码path.join(process.cwd(), 'posts'); the folder path is resolved.文件夹路径已解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM