What's the difference between `fs.readFileSync` and `fs.readFile` regarding 'ENOENT'?

Responsive Ad Header

Question

Grade: Education Subject: Support
What's the difference between `fs.readFileSync` and `fs.readFile` regarding 'ENOENT'?
Asked by:
85 Viewed 85 Answers

Answer (85)

Best Answer
(402)
`fs.readFileSync` synchronously blocks the Node.js event loop until the file is read or an error occurs. If the file doesn't exist, it will throw an `ENOENT` error. `fs.readFile` is asynchronous and doesn't block the event loop. If the file doesn't exist, it will return a Promise that resolves with an `ENOENT` error. This allows your application to continue running while the file is being checked.