1const fs = require("fs"); // Or `import fs from "fs";` with ESM
2if (fs.existsSync(path)) {
3 // Do something
4}
1const fs = require("fs"); // Or `import fs from "fs";` with ESM
2if (fs.existsSync(path)) {
3 // Do something
4}
5
1const fs = require('fs');
2
3let file = '../path/to/chad_warden.mpeg';
4
5// async
6const fileExists = (file) => {
7 return new Promise((resolve) => {
8 fs.access(file, fs.constants.F_OK, (err) => {
9 err ? resolve(false) : resolve(true)
10 });
11 })
12}
13
14// sync
15const fileExistsSync = (file) => {
16 try {
17 fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK);
18 return true;
19 } catch (err) {
20 return false;
21 }
22}
1function executeIfFileExist(src, callback) {
2 var xhr = new XMLHttpRequest()
3 xhr.onreadystatechange = function() {
4 if (this.readyState === this.DONE) {
5 callback()
6 }
7 }
8 xhr.open('HEAD', src)
9}