Im running a simple readfile command, for a video tutorial, and this is the code exactly as the instructor saved it…
var fs = require("fs");
console.log("Starting");
fs.readFile("./sample.txt", function(error, data) {
console.log("Contents: " + data);
});
console.log("Carry on executing");
i have the sample.txt in the same folder as this js file,
and in the sample.txt file i have “This is a sample output for this text document”,
unfortunately i get a “undefined” as the output for the data variable in the code.
If anybody has any insight as to why this is happening, it would be wonderful if someone would help….
THANKS
Try checking if the file exists first:
If it doesn’t exists, check the path, filename and extension, because your code is OK.