Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Sahil Kapoor
Sahil Kapoor
8,932 Points

How to get input in the console in node.js

To produce output in the workspaces or command prompt we use "console.log()" so how can we take input using them in node.js?

1 Answer

Andrew Hickman
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Hickman
Full Stack JavaScript Techdegree Student 10,013 Points

Here is a neat little module that I didn't know about: readline-sync. Assuming you don't need something asynchronous, this might be your golden ticket. Works great for me. Found it from this Stack Overflow question: https://stackoverflow.com/questions/19718669/nodejs-how-to-take-in-text-input-from-a-keyboard-and-store-it-into-a-variable-i

# npm install readline-sync

var readline = require('readline-sync');

var name = readline.question("What is your name?");

console.log("Hi " + name + ", nice to meet you.");
Sahil Kapoor
Sahil Kapoor
8,932 Points

Thanks for the solution this is really a lot simple than some other methods that were over my head..... Thanks a lot.