“Node FS existe” Réponses codées

fs.writefile

const fs = require('fs');

fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
}); 

// Or
fs.writeFileSync('/tmp/test-sync', 'Hey there!');
Jeff Spicoli

Node FS existe

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
Distinct Dingo

Le fichier Nodejs existe

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`)
God Of Coding

Réponses similaires à “Node FS existe”

Questions similaires à “Node FS existe”

Plus de réponses similaires à “Node FS existe” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code