在使用fs.read时没有返回值到console.log

你好,首先,我是新的节点JS,需要帮助在阅读文本文件“information.txt”,并显示在console.log中的值

这是代码:fs.readFileSync(“information.txt”,“utf-8”,函数(err,data){if(err){throw err;} var content = data; console.log(content);} );

你的代码有一些错误,这是一个工作版本:

 var fs = require('fs'); var content = fs.readFileSync("themes", "utf-8"); console.log(content); 

你不能在同步函数中使用callback

修改function

 fs.readFile("information.txt", "utf-8", function (err, data) { if (err) { throw err; } var content = data; console.log(content); });