Next.js Discord

Discord Forum

Error: 'No such file or directory' when using `fs.readFileSync`

Unanswered
Nelson posted this in #help-forum
Open in Discord
### Link to the code that reproduces this issue

https://github.com/tszhong0411/fs-test

### To Reproduce

1. Visit https://fs-test-beta.vercel.app/api/og/test
2. Observe the encountered error.

### Current Behavior
An error message stating "No such file or directory" is displayed when using the fs.readFileSync function.
But the weird thing is that if we uncomment the below line, the error is gone.

const debug = {
  process_cwd: process.cwd(),
  ls_pwd: fs.readdirSync(process.cwd()),
  // ls_public: fs.readdirSync(path.resolve('./public'))
}


Here is a production with no errors: https://fs-test-2.vercel.app/api/og/test
For further information, if we visit https://fs-test-2.vercel.app/api/og/test?log=true, we can see the "public" folder exists.

I think the public/background-image.jpg will not be available if we don't use it somewhere.

### Expected Behavior
No error should occur, and the application should respond with an Open Graph (og) image.

I have already submitted an issue on GitHub [#66168](https://github.com/vercel/next.js/issues/66168).

I would greatly appreciate assistance with investing in it and resolving any potential issues.

0 Replies