This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the sixth post in my new node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming, the fifth was about hashish - hash combinators library.

This time I'll introduce you to a brand new module called read. Read was created just yesterday by Isaac Z. Schlueter, the author of npm. Read is basically read(1) for node. You can read input from stdin with it easily.

Here is an example,

var read = require('read');

read({ prompt : 'Username: ' }, function (err, user) {
  read({ prompt : 'Password: ', silent : true }, function (err, pass) {
    console.log(user, pass);

Nesting functions like this is ugly, so you better use seq, the asynchronous flow combinators library, by James Halliday. (I'll introduce you to this module some time soon.) Here is how it looks with seq:

var read = require('read');
var Seq = require('seq');

  .seq(function () {
    read({ prompt : 'Username: ' }, this.into('user'));
  .seq(function () {
    read({ prompt : 'Password: ', silent : true }, this.into('pass'));
  .seq(function (pass) {
    console.log(this.vars.user, this.vars.pass);

Here are all the options read supports:

prompt  - What to write to stdout before reading input.
silent  - Don't echo the output as the user types it.
num     - Max number of chars to read from terminal.
delim   - The char that means we're done. Default: "\n"
timeout - Number of ms to wait for user input before giving up.

Note: If silent is true, or num is set, or delim is something other than "\n", then read will set raw mode, and read character by character.

You can install read through npm as always:

npm install read

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!


If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->


December 08, 2011, 15:17

I just wanted to note that I am really enjoying this series. Unfortunately, my real job does not provide the excuse to monitor the plethora of modules that are out there, so it's nice to read through your brief overviews. Thanks a lot!

December 08, 2011, 15:19

You're welcome!

Antony Permalink
December 08, 2011, 15:52

I also enjoy your posting much. Just a heads up though, perhaps, when you cover seq in near future, can you do a comparison to another library called "step" ?

I find seq and step achieving very similar goal, but if there is any minor differences (besides different author), I would love to know. Thanks

P.S. Glad there is no Pow! this time around.

December 08, 2011, 22:23

I sure can compare it with step! I know both of them.

December 08, 2011, 18:00

I am really enjoying this series. Would you mind explaining how to achieve the same thing without using the module as well and mentioning some use cases too?

nisc Permalink
December 08, 2011, 21:45

Just have a look at read's source.

It's right inside your ./node_modules ;-)

December 08, 2011, 22:25

Yeah, just take a look at the source code of read. Here:

Jeremy Voorhis Permalink
December 08, 2011, 18:08

Just a heads up, your link for seq actually points to this blog post on read.

December 08, 2011, 22:26


December 08, 2011, 19:21

Why don't the "seq" callbacks need "err" parameters?

December 08, 2011, 22:27

Because it abstracts err. It actually handles it internally and you can use `.catch(cb)` to catch the first 'err' that happens!

Leave a new comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "coding_288": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.