These let your Node.js program read what a user types and show messages on the screen. They help your program talk with people.
process.stdin and process.stdout in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
process.stdin.on('data', (input) => { // handle input here }); process.stdout.write('message');
process.stdin listens for user input from the keyboard.
process.stdout.write() prints messages to the terminal without adding a new line automatically.
Examples
Node.js
process.stdin.on('data', (input) => { console.log('You typed:', input.toString()); });
Node.js
process.stdout.write('Hello! Please type something: ');Node.js
process.stdin.setEncoding('utf8'); process.stdin.on('data', (input) => { if(input.trim() === 'exit') { process.stdout.write('Goodbye!\n'); process.exit(); } else { process.stdout.write(`You said: ${input}`); } });
Sample Program
This program asks the user for their name, then greets them with a message and ends.
Node.js
process.stdout.write('What is your name? '); process.stdin.setEncoding('utf8'); process.stdin.on('data', (input) => { const name = input.trim(); process.stdout.write(`Hello, ${name}! Nice to meet you.\n`); process.exit(); });
Important Notes
Remember to call process.stdin.setEncoding('utf8') to get readable text instead of raw bytes.
Use process.exit() to stop the program after you finish reading input.
process.stdout.write() does not add a new line automatically, so add \n if you want one.
Summary
process.stdin reads what the user types.
process.stdout shows messages on the screen.
They help make interactive command-line programs.
Practice
1. What does
process.stdin do in a Node.js program?easy
Solution
Step 1: Understand the role of process.stdin
process.stdinis used to read data from the terminal where the user types input.Step 2: Differentiate from process.stdout
process.stdoutis for output, not input. File system and network are unrelated here.Final Answer:
It reads input typed by the user in the terminal. -> Option BQuick Check:
Input reading = C [OK]
Hint: Remember: stdin = input, stdout = output [OK]
Common Mistakes:
- Confusing stdin with stdout
- Thinking stdin writes output
- Mixing input with file or network operations
2. Which of the following is the correct way to write 'Hello World' to the terminal using
process.stdout?easy
Solution
Step 1: Identify correct method for output
process.stdout.write()is the method to write output to the terminal.Step 2: Check syntax and usage
process.stdinis for input, not output.console.readandprocess.stdout.readare invalid.Final Answer:
process.stdout.write('Hello World\n'); -> Option CQuick Check:
Output uses stdout.write = A [OK]
Hint: Use stdout.write() to print text, not stdin [OK]
Common Mistakes:
- Using stdin to write output
- Using non-existent console.read method
- Confusing read and write methods
3. What will the following Node.js code output if the user types 'NodeJS' and presses Enter?
process.stdin.on('data', (data) => {
process.stdout.write('You typed: ' + data);
});medium
Solution
Step 1: Understand data event input
Thedataevent receives a Buffer including the newline characters from pressing Enter, usually\r\n.Step 2: Output includes raw input
Concatenatingdatadirectly includes the newline characters, so output ends withNodeJS\r\n.Final Answer:
You typed: NodeJS\r\n -> Option AQuick Check:
Input includes newline chars = B [OK]
Hint: Data event includes Enter key chars like \r\n [OK]
Common Mistakes:
- Assuming input has no newline characters
- Expecting trimmed input automatically
- Confusing syntax errors with output format
4. Identify the error in this code snippet that tries to read user input and print it:
process.stdin.on('data', function(input) {
console.log(input.toString);
});medium
Solution
Step 1: Check method usage on input
input.toStringis a method and needs parentheses to call it:input.toString().Step 2: Verify event and method correctness
dataevent is correct for reading input. Usingconsole.logis valid for output.Final Answer:
Missing parentheses after toString method call. -> Option DQuick Check:
Method calls need () = D [OK]
Hint: Call methods with () to avoid undefined output [OK]
Common Mistakes:
- Forgetting parentheses on toString
- Thinking event name is wrong
- Believing console.log can't print input
5. You want to create a Node.js program that asks the user for their name, then prints 'Hello, [name]!' and exits. Which code snippet correctly uses
process.stdin and process.stdout to achieve this?hard
Solution
Step 1: Prompt user correctly
process.stdout.writeis used to show the prompt without newline.Step 2: Read input and trim newline
data.toString().trim()converts input buffer to string and removes newline characters.Step 3: Output greeting and exit
Print greeting with template string and callprocess.exit()to end program.Final Answer:
Code snippet A correctly prompts, reads, trims, outputs, and exits. -> Option AQuick Check:
Prompt + trim input + exit = A [OK]
Hint: Trim input and call process.exit() after output [OK]
Common Mistakes:
- Using stdin.write instead of stdout.write for prompt
- Not trimming input causing newline in output
- Missing process.exit causing program to hang
