Node.js process.stdin Property
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
// Node.js program to demonstrate the // process.stdin Property // Enter any texts ( User input) 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 w3wiki
w3wiki You typed w3wiki
Example 2: Create a JavaScript file and name this file as index.js.
Javascript
// Node.js program to demonstrate the // process.stdin Property process.stdin.on( 'readable' , () => { let chunk; // Use a loop to make sure we read all available data. 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
Contact Us