How to read all text from stdin to a string with Node.js?

Sometimes, we want to read all text from stdin to a string with Node.js.

In this article, we’ll look at how to read all text from stdin to a string with Node.js.

How to read all text from stdin to a string with Node.js?

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.

For instance, we write

const fs = require('fs');
const data = fs.readFileSync(0, 'utf-8');

to call readFileSync with 0 to return the text from stdin as a 'utf-8' string.

Conclusion

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.