Error: ENOENT: no such file or directory, open '/var/task/src/content/hello.mdx' – Next.js blog Vercel production deployment error

huangapple go评论108阅读模式
英文:

Error: ENOENT: no such file or directory, open '/var/task/src/content/hello.mdx' - Next.js blog Vercel production deployment error

问题

抱歉,以下是您要翻译的部分:

"I am building a blog section for my personal website. I am not able to open a blog post due to the following error:"

"I get the following error message when i visit a blog post on my portfolio:"

"Error shows that PostPage file has error in the code. I have written the following code:"

"I have tried changing path in readFileSync() but nothing works in production. It is working in local dev mode. Kindly Help."

"Please note that i am a beginner in programming and nextjs or vercel. Thanks"

英文:

I am building a blog section for my personal website. I am not able to open a blog post due to the following error:

  1. (Vercel logs)
  2. Error: ENOENT: no such file or directory, open '/var/task/src/content/hello.mdx'
  3. at Object.openSync (node:fs:601:3)
  4. at Object.readFileSync (node:fs:469:35)
  5. at PostPage (/var/task/.next/server/app/posts/[slug]/page.js:381:45)
  6. at S (/var/task/.next/server/chunks/789.js:6503:13)
  7. at eb (/var/task/.next/server/chunks/789.js:6618:21)
  8. at Array.toJSON (/var/task/.next/server/chunks/789.js:6422:20)
  9. at stringify (<anonymous>)
  10. at pb (/var/task/.next/server/chunks/789.js:6829:9)
  11. at mb (/var/task/.next/server/chunks/789.js:6728:29)
  12. at Timeout._onTimeout (/var/task/.next/server/chunks/789.js:6553:16) {
  13. errno: -2,
  14. syscall: 'open',
  15. code: 'ENOENT',
  16. path: '/var/task/src/content/hello.mdx'
  17. }

I get the following error message when i visit a blog post on my portfolio:

> Application error: a server-side exception has occurred (see the server logs for more information).
Digest: 2113801254

Project directory structure:

  1. my-app/
  2. └── src/
  3. ├── app/
  4. ├── posts/
  5. ├── page.js
  6. └── [slug]/
  7. └── page.js or PostPage
  8. └── page.js
  9. ├── layout.js
  10. ├── page.js
  11. ├── components
  12. ├── content/
  13. └── hello.mdx
  14. └── fonts

Error shows that PostPage file has error in the code. I have written the following code:

  1. export default function PostPage({ params }) {
  2. const post = fs.readFileSync(
  3. path.join(__dirname, "../../../../../src/content/" + params.slug + ".mdx"),
  4. "utf-8"
  5. );
  6. const { data: frontmatter, content } = matter(post);
  7. const blog = DOMPurify.sanitize(
  8. marked.parse(content, { mangle: false, headerIds: false })
  9. );
  10. .....
  11. //returns jsx with markdown content
  12. }

I have tried changing path in readFileSync() but nothing works in production. It is working in local dev mode. Kindly Help.

Please note that i am a beginner in programming and nextjs or vercel. Thanks

答案1

得分: 0

你在这里使用了错误的路径 -> path.join(__dirname, "../../../../../src/content/" + params.slug + ".mdx"),'__dirname' 是你的根目录。尝试记录 __dirname 或者你应该使用 process.cwd()

英文:

You are using wrong path here -> path.join(__dirname, "../../../../../src/content/" + params.slug + ".mdx"), '__dirname' is your root folder. Try logging __dirname or you should use process.cwd()

huangapple
  • 本文由 发表于 2023年7月27日 23:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定