我正在运行一个简单的readfile命令,用于视频教程,这是完全按照导师保存的代码...
var fs = require("fs");
console.log("Starting");
fs.readFile("./sample.txt", function(error, data) {
console.log("Contents: " + data);
});
console.log("Carry on executing");
我在与此js文件相同的文件夹中有一个sample.txt文件,在sample.txt文件中我有"This is a sample output for this text document",不幸的是,代码中的data变量输出为"undefined"。 如果有人知道为什么会发生这种情况,希望能提供帮助... 谢谢。
if (error) throw error;
,看看能否得到任何提示。 - Pointyconsole.log(error)
。) - PointyENOENT
的意思是文件不存在。 - Pointyls 'C:\ Users \ Jenny Kemp \ sample.txt'
时,您是否看到该文件? - Pointy