Answers for "node js fs file exists"

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
1

node fs exists

const { promises: Fs } = require('fs')

async function exists (path) {  
  try {
    await Fs.access(path)
    return true
  } catch {
    return false
  }
}

// Example:
const Path = require('path')  
const path = Path.join(__dirname, "existing-file.txt")

await exists(path)  
// true
Posted by: Guest on July-07-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
2

node check if file exists

const fs = require('fs');

let file = '../path/to/chad_warden.mpeg';

// async
const fileExists = (file) => {
    return new Promise((resolve) => {
        fs.access(file, fs.constants.F_OK, (err) => {
            err ? resolve(false) : resolve(true)
        });
    })
}

// sync
const fileExistsSync = (file) => {
    try {
        fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK);
        return true;
      } catch (err) {
        return false;
      }
}
Posted by: Guest on March-16-2020
-1

check if file exists javascript

function executeIfFileExist(src, callback) {
    var xhr = new XMLHttpRequest()
    xhr.onreadystatechange = function() {
        if (this.readyState === this.DONE) {
            callback()
        }
    }
    xhr.open('HEAD', src)
}
Posted by: Guest on May-20-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language