Answers for "How to check if a file exists in fs"

18

node check if file exists

const fs = require("fs"); // Or `import fs from "fs";` with ESM
if (fs.existsSync(path)) {
    // Do something
}
Posted by: Guest on May-22-2020
2

node if file exists

const fs = require("fs"); // Or `import fs from "fs";` with ESM
if (fs.existsSync(path)) {
    // Do something
}
Posted by: Guest on January-13-2021
1

nodejs file exists

const fs = require('fs')
// We will convert sync function into a promise function
// so when is ready will provide the result without blocking.
const exists = async (path) => {
	return await new Promise((resolve) => {
		resolve(fs.existsSync(path));
	});
};
// If you have a file name samples on same root it will result true.
exists('./samples.txt').then(res => console.log(res))
console.log(`I'm not blocked as I'll show up on first`)
Posted by: Guest on September-25-2021

Code answers related to "How to check if a file exists in fs"

Code answers related to "Javascript"

Browse Popular Code Answers by Language