Я пытаюсь прочитать файл построчно.
После этот ответ , вот мой код:
const fs = require("fs");
const readline = require("readline");
const stream = fs.createReadStream("file.txt");
stream.on("error", err => console.log("Error: " + err));
const reader = readline.createInterface({input: stream});
reader.on("line", function (line) {
console.log(line);
});
Однако ничего не печатается.
Вот содержимое объекта reader
:
Interface {
_sawReturnAt: 0,
isCompletionEnabled: true,
_sawKeyPress: false,
_previousKey: null,
domain: null,
_events:
{ close: { [Function: bound onceWrapper] listener: [Function: onSelfCloseWithoutTerminal] } },
_eventsCount: 1,
_maxListeners: undefined,
output: undefined,
input:
ReadStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 65536,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: true,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: { end: [Array], error: [Function], data: [Function: ondata] },
_eventsCount: 3,
_maxListeners: undefined,
path: 'file.txt',
fd: null,
flags: 'r',
mode: 438,
start: undefined,
end: Infinity,
autoClose: true,
pos: undefined,
bytesRead: 0 },
historySize: 30,
removeHistoryDuplicates: false,
crlfDelay: 100,
_prompt: '> ',
terminal: false,
_decoder:
StringDecoder {
encoding: 'utf8',
fillLast: [Function: utf8FillLast],
lastNeed: 0,
lastTotal: 0,
lastChar: <Buffer 2d 00 00 00> } }
Что именно мне здесь не хватает?
Спасибо!