1const fs = require("fs"); // Or `import fs from "fs";` with ESM
2if (fs.existsSync(path)) {
3 // Do something
4}
1const fs = require('fs')
2
3const path = './file.txt'
4
5try {
6 if (fs.existsSync(path)) {
7 //file exists
8 }
9} catch(err) {
10 console.error(err)
11}
12
1const fs = require('fs')
2
3const path = './file.txt'
4//Async method
5fs.access(path, fs.F_OK, (err) => {
6 if (err) {
7 console.error(err)
8 return
9 }
10
11 //file exists
12})
13
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}