Get user input with javascript

so i searched for how to get user input, and I found this code:

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout

rl.question("What is your name ? ", function(name) {
    rl.question("Where do you live ? ", function(country) {
        console.log(`${name}, is a citizen of ${country}`);

rl.on("close", function() {
    console.log("\nBYE BYE !!!");

now I know I am not capable of understanding the whole code yet, but when I used it on my nodejs that I installed from nodejs website, it prompt me a question what your name? when I type my name the letters are typed twice like this zzaakk , and when I hit enter the nodejs window disappear. why?
thank you.

Is this the only time you are using createInterface in your code? According to this stackoverflow thread this can happen if you have multiple instances of createInterface listening to process.stdin. If this isn’t the issue then perhaps it’s an issue with the terminal you are using and you could try a different terminal? What OS are you using and which terminal?

1 Like

It is just a nodejs terminal i guess, just a command prompt that opens when i double click on the nodejs icon that showed up when i installed it like you proposed some days ago.
I have lenovo laptop that runs on microsoft windows 10.

Just to add like i said earlier, those keywords on that thread of code i shared, that was the first time i see them i don’t know what they do. all i wanted to do is to test a boolean function with an input of a user"me" and see if the boolean will evaluate the input as true or false.
I did that on developer tool of chrome browser using thes 2lines of code:
Var name= window.prompt();
Then i typed an empty string (since empty strings evaluates to false in boolean function), but it returned true. Actually any thing i type as an input using that line of code, the boolean function returns true.

I just want to ask one more time, is this the only time in your code that you are calling readline.createInterface? I put just the code you pasted above into a file and ran it through node on my computer and it worked just fine. Is the code you pasted above the only code in your file?

Yes it is the only code in file!? I think i still don’t know how to run a file of code through nodejs or any terminal. All i did was i copied the code from the internet then pasted it in the terminal (i typed .editor in the nodejs terminal hit enter, the terminal turns into an editor then you can write code like on visual studio).

Ahh, that’s what I needed to know. Ya, you don’t want to run this code that way. You have saved the code in a file. You should be able to run it from the command line with node [name_of_file].

So if the name of the file is mycode.js then type the following at the command prompt and hit Enter (assuming you are in the same directory as the file is stored):

node mycode.js

You can’t type an empty string, you can type nothing and it will be an empty string.

// false
Boolean(' ')
// true

so i did what you said and I got this:
is it because I am not in the same directory as the file is stored? how to be in the same directory as the file?

yes you need to open the file from its folder

or you can write the path to the file too

my file is on the desktop, not in any folder.
can you give an exemple.
thank you.

You don’t want to type node getuserinput.js inside of the node prompt, you want to type it at the command prompt of the terminal.

How to open command prompt

And yes, you need to cd to the directory where your JS file is stored before typing node getuserinput.js.

You can also do this through Visual Code Studio if you are using that for your editor.

MS Tutorial for Running NodeJS with VSC

1 Like

i found 2 command prompts one i got by using cmd in the windows search and another that is not the node propt i use but a nodejs command prompt:

and I don’t know how to cd to the directory where my js file is, this is the first time I hear of it. google search is not helping.

If you go inside the folder with the file and right-click (or Shift + right-click) in an empty space inside the folder your context menu should have some sort of the Open in/Open with “name of your console”. It will open the command line console/terminal inside that folder.

It shouldn’t matter which one you use as long as NodeJS is globally accessible (which it should be).

that is it, thank you bbsmooth, it all worked out.
i can see results on vs terminal now.
create directories and files with command prompt and run the files and see results on the terminal.

One more thing, if the nodejs was not installed on my computer, will a node filename.js still give results in the terminal?

if node is not installed you can’t run a js file from the terminal

1 Like

And so i guess it is the same for every code language right?
Like later on when i start leaning python on freecodecamp i need to install something related to python so its files can run from the VSC terminal for exemple?

JS and python are interpreted languages which means that in order to execute the code you write you need something to read and execute the code you write. In the case of JS this something is usually referred to as a JavaScript Engine. Your browser has one built into it. NodeJS is a free-standing engine. Python also has the equivalent of a JS engine. So yes, you will need the python interpreter installed on your computer in order to run any python code just like you needed to intall NodeJS to run JS code from the command line.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.