How to search if a file exists in JavaScript?

I’m trying to make a script that detects a file but how do I do that?

Can you try something like this?

const fs = require('fs');

var file = "content.json"

try {
    if(fs.existsSync(file)) {
        console.log(file + " was found!");
    } else {
        console.log(file + " could not be found, sorry!");
    }
} catch (err) {
    console.error(err);
}
1 Like