Skip to content

Instantly share code, notes, and snippets.

@huguangju
Last active May 25, 2016 02:45
Show Gist options
  • Save huguangju/d365bd1c459b50cec74630a1919781d0 to your computer and use it in GitHub Desktop.
Save huguangju/d365bd1c459b50cec74630a1919781d0 to your computer and use it in GitHub Desktop.
Check if a file no exist in NodeJS, create it
var fs = require('fs');
module.exports = function checkFileAndCreate(cb) {
try {
fs.accessSync(file, fs.F_OK);
cb(null);
} catch (err) {
if(err.code === 'ENOENT') {
fs.writeFileSync(file, '');
cb(null);
}else {
cb(err);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment