如何读取Node.js中的文件?

use*_*495 6 javascript filesystems file fs node.js

在Node.js中,我想读取一个文件,然后console.log()将文件的每一行分开\n.我怎样才能做到这一点?

ste*_*ewe 7

试试这个:

var fs=require('fs');

fs.readFile('/path/to/file','utf8', function (err, data) {
  if (err) throw err;
  var arr=data.split('\n');
  arr.forEach(function(v){
    console.log(v);
  });
});
Run Code Online (Sandbox Code Playgroud)