Why is this happening?
The readable event is thrown not only when a new piece of data is available, but also when the end of the stream is reached:
Here is what is said in the official documentation :
This is a "readable" event.
Further, if you try to read from the stream after the end of the stream has been reached, the stream.read method will return null .
Let me quote the documentation again:
Note: Calling stream.read([size]) after the 'end' event has been returned. No runtime error will be raised.
In other words, a null return is the regular behavior of a readable stream.
What to do?
There are several options here:
The stream.read method returns null only one case. You can explicitly check the result of stream.read for this value:
process.stdin.on('readable', () => { let chunk = process.stdin.read(); if (null !== chunk) { console.log(chunk.toString()); } });
You can use the stream in a different ( flowing ) mode and receive data on the data event:
process.stdin.on('data', (chunk) => { console.log(chunk.toString()); });