Reputation: 467
I have a function that tests whether a file exists or not before editing it. I use fs.stat.
fs.stat('../fill/bower.json', function (err, stats) {
if (err) {
console.log('You don\'t have a ' + clc.red('bower.json') + ' file! Type ' + clc.bgBlack.white('touch bower.json') + ' to get started.');
return;
} if (stats.isFile()) {
var json = JSON.parse(fs.readFileSync('../bower.json', 'utf8')),
string = '\n Dependencies: ' + json;
fs.writeFile('../fill/README.md,', string, 'utf8');
console.log('it\'s saved!');
}
})
However, every time I run it (bower.json
doesn't exist on purpose), it returns undefined
before You don't have a bower.json file!
. Why does this happen and how can I stop the function printing undefined
?
Edit: for reference, here's my terminal window after running the command:
Why is undefined
printed, and what do I do to have that not be displayed?
Upvotes: 1
Views: 949
Reputation: 47172
You're returning nothing or undefined
from your reading
function.
Upvotes: 2