if file exists js code example
Example 1: node if file exists
const fs = require("fs"); // Or `import fs from "fs";` with ESM
if (fs.existsSync(path)) {
// Do something
}
Example 2: javascript file exists check
// checking existence of file synchronously
function doesFileExist(urlToFile) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
return xhr.status !== 404;
}