Nodejs readline给出了一些错误

我想逐行阅读一个文件,并根据每行的数据,我必须对数据进行分类。

var rd = readline.createInterface({ input: fs.createReadStream('/home/user/Desktop/text.txt'), output: process.stdout, console: false }); 

当这行被执行时,文件被读取并打印在terminal中。

但是当我尝试使用readline逐行读取文件时,出现错误。

 rd.on('line', (input) => { console.log(input); }); 

我收到以下错误。

 `Interface { _sawReturnAt: 0, isCompletionEnabled: true, _sawKeyPress: false, _previousKey: { sequence: '\n', name: 'enter', ctrl: false, meta: false, shift: false }, domain: Domain { domain: null, _events: { error: [Function: debugDomainError] }, _eventsCount: 1, _maxListeners: undefined, members: [] }, _events: { line: [ [Function], [Function], [Function] ] }, _eventsCount: 1, _maxListeners: undefined, output: WriteStream { connecting: false, _hadError: false, _handle: TTY { bytesRead: 0, _externalStream: {}, fd: 9, writeQueueSize: 0, owner: [Circular], onread: [Function: onread] }, _parent: null, _host: null, _readableState: ReadableState { objectMode: false, highWaterMark: 16384, buffer: [Object], length: 0, pipes: null, pipesCount: 0, flowing: null, ended: false, endEmitted: false, reading: false, sync: true, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, defaultEncoding: 'utf8', ranOut: false, awaitDrain: 0, readingMore: false, decoder: null, encoding: null }, readable: false, domain: null, _events: { end: [Object], finish: [Function: onSocketFinish], _socketEnd: [Function: onSocketEnd], resize: [Object] }, _eventsCount: 4, _maxListeners: undefined, _writableState: WritableState { objectMode: false, highWaterMark: 16384, needDrain: false, ending: false, ended: false, finished: false, decodeStrings: false, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: false, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 1, prefinished: false, errorEmitted: false, bufferedRequestCount: 0, corkedRequestsFree: [Object] }, writable: true, allowHalfOpen: false, destroyed: false, _bytesDispatched: 22151, _sockname: null, _writev: null, _pendingData: null, _pendingEncoding: '', server: null, _server: null, columns: 80, rows: 24, _type: 'tty', fd: 1, _isStdio: true, destroySoon: [Function], destroy: [Function] }, input: ReadStream { _readableState: ReadableState { objectMode: false, highWaterMark: 65536, buffer: [Object], length: 0, pipes: null, pipesCount: 0, flowing: false, ended: true, endEmitted: true, reading: false, sync: false, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, defaultEncoding: 'utf8', ranOut: false, awaitDrain: 0, readingMore: false, decoder: null, encoding: null }, readable: false, domain: Domain { domain: null, _events: [Object], _eventsCount: 1, _maxListeners: undefined, members: [] }, _events: { end: [Object], data: [Function: onData] }, _eventsCount: 2, _maxListeners: undefined, path: '/home/user/Desktop/text.txt', fd: null, flags: 'r', mode: 438, start: undefined, end: undefined, autoClose: true, pos: undefined, bytesRead: 31, destroyed: true, closed: true }, historySize: 30, crlfDelay: 100, _prompt: '> ', terminal: true, line: '', cursor: 0, history: [ '1,2,3', '4,8,4,8', '2,2,2,2', '1,2,3,4' ], historyIndex: -1, prevRows: 0, paused: true, closed: true }` 

请帮助我对此是新的。

你的代码看起来很好,但是你正在使用Node.js REPL而不是从文件运行代码,这就是为什么你会看到这种行为。

Node.js REPL(你在terminalinputnode时得到的)将会存储variables。 但是,只有使用标识符时,该值也会返回。 你看到的不是一个错误,而是rd对象。

Node.js REPL非常适合testing,但除非您有特定的原因使用它,否则最好的方法是创build一个app.js文件,将代码添加到它,然后运行node app.js

如果您确实需要使用REPL编写多行代码,那么一定要在初始化时键入.editor ,以便正确解释空格等。

而且,从7.7.2文档console不是createInterface期望的选项,以便可以删除。 在您的特定示例中,您还可以删除output: process.stdout ,作为使用line事件logging每行