Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
You can check if a file exists using the fs module. Besides the methods already mentioned, you can use fs.stat(path[, options], callback) [1]. It's also asynchronous, and the callback returns two ...
Answer
#1: Initial revision
You can check if a file exists using the `fs` module. Besides the methods already mentioned, you can use `fs.stat(path[, options], callback)` [^1]. It's also asynchronous, and the callback returns two arguments `(err, stats)`. ```javascript import { stat } from "node:fs"; stat("/path/to/file.ext", (err, stats) => { if (err) { // Something happened; check err.code to get details } else { // File exists } }); ``` [^1]: Refer to https://nodejs.org/api/fs.html#fsstatpath-options-callback