-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functions to read from stdin #37925
Comments
Hi, thank you for your feature request. There is a Readline module in Node.js. You could try If it does not fit your need, feel free to share your view here please. |
Regarding having a synchronous API, I don't think it would very useful now that we have support for top-level If #37287 was implemented, you could do: import { createInterface } from 'node:readline/promises';
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'OHAI> '
});
const name = await rl.question("What's your name?");
console.log(`Hello ${name}!`); If you wanted to implement it today, you can use import { createInterface } from 'node:readline';
import util from 'node:util':
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'OHAI> '
});
export const input = util.promisify(rl.question).bind(rl); |
Hello @Ayase-252, Thank you for pointing me to I'm not saying there is no way to get user input from node, I'm saying that the way to do it is unnessecarily long and complicated for beginner programmers. Getting user input in Python: name = input("Enter your name") Getting user input in C++: cout << "Enter your name"
cin >> name Meanwhile, getting user input in JS: const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What is your name?", name =>{
console.log(`Hello, ${name}!`);
rl.close();
}) The fact that you need all this boilerplate code for something as basic and imperative to a general purpose programming language as getting user input instead of just using one simple function like you do in Python is what I'm arguing against. The solution @aduh95 provided wouldn't work either because you can only use promisify on functions that are |
https://nodejs.org/api/readline.html#readline_rl_question_query_options_callback |
Ah, apologies. I didn't know that, my bad. |
@anirudhgiri I'm neutral to implement the feature, but I would argue that complexity in some degree is unavoidable for new player in a new technology. It reflects me as a completely naive learner without any prior knowledge about programming as a freshman in university. I was tought C as my first programming language. I have absolutely no idea about what With C, the minimal CLI program will be something like #include <stdio.h>
int main() {
char fav_food[100];
printf("What's your favorite food?");
scanf("%s", fav_food);
printf("That is your food" + fav_food);
return 0;
} it would confuse a lot people who are new to programming from my experience. the As @aduh95 and @Linkgoron mentioned, thanks to awesome top-level import utils from 'node:util';
import { createInterface } from 'node:readline';
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const input = utils.promisify(rl.question).bind(rl);
const favFood = await input('What is your favorite food?\n');
console.log(`That is your food: ${favFood}`); Still too complex? We can hide the // input.mjs
import utils from 'node:util';
import { createInterface } from 'node:readline';
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
export const input = utils.promisify(rl.question).bind(rl); In the learner side, the only thing she/he needs to do is // main.mjs
import { input } from './input.mjs'
const favFood = await input('What is your favorite food?\n')
console.log(`That is your food: ${favFood}`) Then run ➜ node git:(triaging/main) ✗ node main.mjs
What is your favorite food?
Sushi!
That is your food: Sushi! I think it is much simpler and explainable than a C example for new learners. Edit: multiple grammar problems |
Excellent! Don't you think the short module you just wrote, |
It's alright. Personally, I would perfer to incoperate import { input } from 'node:readline'
const favFood = await input('What is your favorite food?\n')
console.log(`That is your food: ${favFood}`) I think the implementation is simple. But I'm not sure about whether it is right to add one method |
Another possibility could be to add a method to |
Thanks, I did some experiments around In this case, could be |
Hi @anirudhgiri and @Ayase-252, What are your opinions about having that function named
A quick check shows that Deno uses Although I'm not sure whether it may be confused with |
@artembykov I like the idea, it would be ideal. |
@artembykov Sounds good to me! |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
I have just bumped into this while composing a beginner's programming course trying to use node/js as the first platform/language of a rookie. Will probably have to use |
Buffer.concat(await process.stdin.toArray()).toString("utf-8") EDIT: the counter-argument would be to use import { text } from "node:stream/consumers"
await text(process.stdin); As I just discovered in whatwg/streams#1019 (comment) ❗ |
Assuming it works (I haven't tried), all the arguments in the discussion above:
The hopes were that at least this feature would be available as a straightforward function from the get go. |
Yeah, this is purely a "read all stdin", not the readline solution. It's just the shortest/clearest incantation I've found (much better than using |
You have to go back to Node.js 12.x to find a version where top-level |
The Problem
Javascript, used in conjunction with NodeJS, is increasingly being used as a general purpose programming language. While I acknowledge the fact that it was originally intended to be used as a language for server-side development, but the language has grown into something much more general from being used to create executables, GUIs, video games, mobile applications and even operating systems. The world of educational computer science is moving away from teaching beginners languages like C and Java as their first language to teaching them Python and NodeJS instead.
There are some features who's existence in a modern general purpose programming language is imperative like printing to the console, doing basic array and string operations, and getting user input. Unfortunately, getting user input from
stdin through nodejs is an absolute pain. You should either declare a buffer, connect it to the
stdin
stream and use the buffer to read input line-by-line or use third party libraries.Having your users of your language, especially beginners, go through such a process just for the luxury of getting input from the user through the terminal for such a popular and widely used language should simply be unacceptable. Yet, it is the norm.
NodeJS is an available option in online learning platforms like HackerRank and Leetcode, and in hiring software used for coding interviews. Since most input is received through
stdin
, programmers either have to rely on the platform taking care of getting the input and just give the user a function with the inputs given as parameters to work with (like Leetcode does) or the programmers have to write all the boilerplate code to receive user input themselves (and loosing time and competitive edge in the process) like in Google Kickstart. This only drives people away from using Node in such competitive environments to other to use languages like Python (where they can just useinput()
) or C++ (where they can just docin >>
).The solution
Much like C's
scanf()
or Python'sinput()
, Node should have a simple and beginner friendly way to recieve user input from the terminal. My suggestion is two functions -input()
andinputSync()
.input()
andinputSync()
both return a line from stdin as a String but just likereadFile()
andreadFileSync()
,input()
does it asynchronusly whileinputSync()
does it synchronusly.Example Usage
The text was updated successfully, but these errors were encountered: