You are currently viewing How to take user input in nodejs / javascript in 22?
how to take user input in nodejs?

How to take user input in nodejs / javascript in 22?

In this article, we will learn how can we take the user input in nodejs or javascript from file.

Taking Command-line inputs in javascript is not possible, but using nodejs that is javascript run-time environment it is possible.

Last week, I was exploring competitive programming problems and trying to solve them in javascript. I used to code at CodeChef, code forces, spoj, etc in Java but now being a javascript developer, my inner self forced me to try the problem in javascript.

This may be different from others but I prefer to write the code locally and when it works fine only then submit the code to the above platforms.

This article will help you to set up and explain the initial code that will be required to take the user input in node.js.

Before we dive into the implementation, let’s understand the key terms first.

Key functions to understand

process.stdin()

The process.stdin property is available on the process module which listens to the user input. This property stdin is a readable stream and uses the on function to listen for the event.

				
					// Syntax

process.stdin.on()
				
			

process.stdin.resume()

This resumes the readable stream if it was paused previously. This function ensures the readable stream is started before we take the user input.

readline()

The readline() function is available on the node.js Readline module which allows the reading of the input stream line by line.

main()

In traditional programming languages, we see the main function is mandatory because the execution starts from this function only. But this is not the case with javascript.

To make it consistent with the traditional one, we have written the main function here too but you can use whatever function name you like.

The usage of this function is to take and process the user input, call necessary function and print the output to terminal or write in file system.

Implementation

Problem statement

Implement a function that takes 2 input length and breadth from the file and print the output on the terminal.

Solution

Create 2 files input.js which will have the code and input.txt which contains the actual input. These 2 files should be in the same folder.

Check the structure and running in the below replit screenshot.

How to take user input in nodejs?

Above is the index.js code, now in the input.txt we can give the 2 numbers in the input e.g 10 20 (with space separated).

Area will be 10 * 20 = 200

To run the code, type the below in the terminal –

				
					// to print the output on the terminal / console
cat input.txt | node index.js

// to print the output in a file
cat input.txt | node index.js > output.txt
				
			

I hope you like the article, if yes, please do share and leave a reply.

I would also like to make a humble request to disable the adblocker please if it’s on, weekendtutorial tries to earn from the google ads, so disabling the ad blocker for this site would certainly be a great help.

If you want to try to upgrade your DSA skills solve the problem on leetcode

Please share the articles on –

Facebook
Twitter
LinkedIn
Subscribe

The latest tips and news from the industry straight to your inbox!

Join 30,000+ subscribers for exclusive access to our monthly news and tutorials!