Created
December 12, 2018 04:14
-
-
Save kselax/e9f537f0e7ae6c8842a9bc15e5a7e2c9 to your computer and use it in GitHub Desktop.
check the existance a file or a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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