Skip to content

Instantly share code, notes, and snippets.

@Sasha-hk
Created April 9, 2022 00:13
Show Gist options
  • Save Sasha-hk/48a84e25eb9b1370b9c79372132dd787 to your computer and use it in GitHub Desktop.
Save Sasha-hk/48a84e25eb9b1370b9c79372132dd787 to your computer and use it in GitHub Desktop.

Input function in pure Node.js

Code:

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

function input(str) {
  return new Promise(res => {
    readline.question(str, result => {
      res(result);
    });
  })
}

async function start() {
  const username = await input('Enter username: ');
  console.log(`Hello ${username}`);

  readline.close();
}

start()

Run:

node [file_name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment