Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).
Find a file
anseki f4dc86f23f Merge branch 'patch-1' of https://github.com/Grahack/readline-sync into pull-request-master
Conflicts:
	lib/readline-sync.js

Typo about platform not supporting
2014-05-13 21:27:25 +09:00
lib Typo about «platform not supporting…» 2014-05-09 14:04:52 +02:00
.gitignore Initial commit 2013-08-29 19:55:23 +09:00
LICENSE-MIT Initial commit 2013-08-29 19:55:23 +09:00
package.json fixed #1 2013-12-18 13:17:03 +09:00
README.md fixed #1 2013-12-18 13:17:03 +09:00

readlineSync

Synchronous Readline for interactively running.
The interface is used with process.stdin and process.stdout in order to accept user input.

Example

var readlineSync = require('readline-sync');
var answer = readlineSync.question('What is your favorite food? :');
console.log('Oh, so your favorite food is ' + answer);

Installation

npm install -g readline-sync

Usage

setPrompt

readlineSync.setPrompt(prompt)

Sets the prompt, for example when you run node on the command line, you see > , which is node's prompt.

prompt

line = readlineSync.prompt()

Readies readline for input from the user, putting the current setPrompt options on a new line, giving the user a new spot to write.

question

line = readlineSync.question(query)

Displays the query to the user, and then returns the user's response after it has been typed.

setEncoding

readlineSync.setPrompt(encoding)

Set the encoding method of input (user's response) and output (prompt). Defaults to 'utf8'.

Note

The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms.
If in those platforms, an error is thrown.

try {
  answer = readlineSync.question('What is your favorite food? :');
} catch (e) {
  console.error(e);
  process.exit(1);
}

Release History

  • 2013-12-18 v0.2.2 Error handle for the environment which don't support interactively reading from stdin.
  • 2013-08-30 v0.2.0 Rewrite exporting methods.
  • 2013-08-29 v0.1.0 Initial release.