Socket
Socket
Sign inDemoInstall

readline

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    readline

Simple streaming readline module.


Version published
Weekly downloads
1.8M
increased by0.98%
Maintainers
1
Install size
1.87 MB
Created
Weekly downloads
 

Package description

What is readline?

The readline npm package is a core Node.js module that provides an interface for reading data from a Readable stream (like process.stdin) one line at a time. It is particularly useful for creating command-line interfaces and reading large files line by line.

What are readline's main functionalities?

Reading Input from stdin

This code sets up a readline interface to read user input from the standard input (stdin). It asks the user 'What is your name?' and prints a greeting with the user's input.

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
rl.question('What is your name? ', (answer) => {
  console.log(`Hello, ${answer}!`);
  rl.close();
});

Reading a File Line by Line

This example demonstrates how to use readline to read a file line by line. It creates a readline interface with a file stream as input and logs each line to the console as it is read.

const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
  input: fs.createReadStream('file.txt'),
  output: process.stdout,
  terminal: false
});
rl.on('line', (line) => {
  console.log(line);
});

Other packages similar to readline

Readme

Source

readline

Read a file line by line.

Install

Important. In node 10 there is a core module named readline. Please use linebyline instead, it is the same module just renamed:

Npm linebyline

npm install linebyline

Test

npm install .
npm test

What's this?

Simple streaming readline module for NodeJS. Reads a file and buffers new lines emitting a line event for each line.

Usage

Simple

  var readline = require('linebyline'),
      rl = readline('./somefile.txt');
  rl.on('line', function(line, lineCount, byteCount) {
    // do something with the line of text
  })
  .on('error', function(e) {
    // something went wrong
  });

ASCII file decoding

As the underlying fs.createReadStream doesn't care about the specific ASCII encoding of the file, an alternative way to decode the file is by telling the readline library to retain buffer and then decoding it using a converter (e.g. iconv-lite).

  var readline = require('linebyline'),
      rl = readline('./file-in-win1251.txt', {
    retainBuffer: true //tell readline to retain buffer 
  });
  rl.on("line", function (data,linecount){
    var line = iconv.decode(data, 'win1251');
    // do something with the line of converted text
  });

##API

readLine(readingObject[, options])

Params:

  • readingObject - file path or stream object
  • options can include:
    • maxLineLength - override the default 4K buffer size (lines longer than this will not be read)
    • retainBuffer - avoid converting to String prior to emitting 'line' event; will pass raw buffer with encoded data to the callback

Return:

  • EventEmitter

License

BSD © Craig Brookes

Keywords

FAQs

Last updated on 28 Dec 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc