Node.js process.stdin Property
Last Updated :
16 Mar, 2021
The process.stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.
Syntax:
process.stdin.on();
Return Value: It doesn’t return any value.
Parameters: This property takes Input from the user.
Below examples illustrate the use of process.stdin property in Node.js:
Example 1: Create a JavaScript file and name this file as index.js.
Javascript
process.stdin.on( 'data' , data => {
console.log(`You typed ${data.toString()}`);
process.exit();
});
|
Run the index.js file using the following command:
node index.js
Output: Now type any text from the terminal, as shown below we have typed GeeksforGeeks
GeeksforGeeks
You typed GeeksforGeeks
Example 2: Create a JavaScript file and name this file as index.js.
Javascript
process.stdin.on( 'readable' , () => {
let chunk;
while ((chunk = process.stdin.read()) !== null ) {
process.stdout.write(`data: ${chunk}`);
}
});
|
Run the index.js file using the following command:
node index.js
Output: Now type any text from the terminal, as shown below we have typed One, followed by Two and so on.
One
data: One
Two
data: Two
Reference: https://nodejs.org/api/process.html#process_process_stdin
Share your thoughts in the comments
Please Login to comment...