User input in javascript | Sololearn: Learn to code for FREE!

0

User input in javascript

I can't take user input in node.js. It says prompt is not defined. Please help me out to take user input at javascript.

5/18/2021 7:53:33 AM

Aminul Islam

9 Answers

New Answer

+1

https://nodejs.org/en/knowledge/command-line/how-to-prompt-for-command-line-input/

0

prompt() is a browser specific function... node.js is designed to run at server side: what is the purpose of user input in this context?

0

Well, how can I take user input at node.js?

0

I find node's asynchronous nature awkward for user input. To be fair it was not designed for this kind of thing. For writing simple JavaScript command line utilities on windows I prefer to use Microsoft's JScript, which is included with the Windows Script Host (WSH). WScript.StdOut.Write("Enter your name: ") var str = WScript.StdIn.Readline() WScript.StdOut.Write("Hello " + str) The downside is that JScript is old, about JavaScript 1.5 or ECMAScript 3/4. Other alternatives are Rhino (getting old as well) and Nashorn; both of which are implemented in Java. If blinding speed is required then the SpiderMonkey JavaScript engine is impressive. All of these make decent general purpose scripting engines, but I wouldn't use any of them as an alternative to Node on a server.

0

Other synchronous alternatives to readline are readline-sync and prompt-sync https://www.codecademy.com/articles/getting-user-input-in-node-js https://www.npmjs.com/package/readline-sync

0

Prompt-sync doesn't work

0

I've just tested prompt-sync with Microsoft Visual Studio and Node.js and it works fine for me. Did you remember to use npm to install the prompt-sync package?

0

Yes it works at my windows command prompt and I often use prompt-sync while coding. But it doesn't work at sololearn codeplayground and that's why I can't completing the projects of javascript courses which is mandatory to acquire certificate.

0

It won't work in the code playground because you are not at the console in the of the code playground's server. You are on a remote connection. All of the exercises in the course can be completed using nothing more than the lessons given.