Skip to content

Instantly share code, notes, and snippets.

@kselax
Created December 12, 2018 04:14
Show Gist options
  • Save kselax/e9f537f0e7ae6c8842a9bc15e5a7e2c9 to your computer and use it in GitHub Desktop.
Save kselax/e9f537f0e7ae6c8842a9bc15e5a7e2c9 to your computer and use it in GitHub Desktop.
check the existance a file or a directory
// check file or directory existance in async an sync way
const fs = require('fs')
// sync
if (fs.statSync(`${__dirname}/package.json`).isFile()) {
console.log('sync: file');
}
if (fs.statSync(`${__dirname}/.`).isDirectory()) {
console.log('sync: directory');
}
// async
fs.stat(`${__dirname}/package.json`, (err, stats) => {
if (err) throw err
if (stats.isFile()) {
console.log('async: file');
}
})
fs.stat(`${__dirname}/.`, (err, stat) => {
if (err) throw err
if (stat.isDirectory) {
console.log('async: directory');
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment