Home > nodejs > [nodejs] raw_input in Node.js

[nodejs] raw_input in Node.js

How to read in Node.js from the console? For instance, how to rewrite the following Python script?

n1 = int(raw_input("1st number: "))
n2 = int(raw_input("2nd number: "))

print "The sum is:", n1+n2


#!/usr/bin/env node

"use strict";

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout

function processNums(n1, n2) {
    n1 = Number(n1);
    n2 = Number(n2);
    console.log("Their sum: " + (n1 + n2));

function start() {
    rl.question('1st number: ', function (x) {
        rl.question('2nd number: ', function (y) {

            processNums(x, y);

function main() {


Tip from here.

Categories: nodejs Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: