0

I am trying to make a cmd line based program but after realizing chalk uses module type, not the old const module = require('module'); method, I realized I needed a new package for prompting users. How can I easily prompt the user for input in an easy way.

WhatsVape
  • 11
  • 4
  • It is different. That uses a different module system. The way of importing is different and both cannot be used together. – WhatsVape Aug 01 '22 at 03:26
  • 1
    So that means your question is ostensibly **not** as simple as, _"How do I prompt users for input in NodeJS"_ nor is it _"How can I easily prompt the user for input in an easy way?"_ Perhaps you should edit so that you are asking your actual question. You are vaguely hinting perhaps you're having trouble using an ES6 module or that you'd like an alternative. You're going to have to be more specific about what you need and what's going wrong. – Wyck Aug 01 '22 at 03:31
  • I am indeed having a problem with an es6 module. i need an alternative from prompt-sync that is es6 and is as simple as `prompt("name")` – WhatsVape Aug 01 '22 at 03:33
  • 1
    What did you try? Did you try [_dynamic import_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)? See [this answer](https://stackoverflow.com/a/71722204/1563833) for an example of how to import an ES Module from Common JS. – Wyck Aug 01 '22 at 03:47

1 Answers1

1

You can use readline built-in module

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

readline.question(`What's your name?`, name => {
  console.log(`Hi ${name}!`);
  readline.close();
});
Slimane amiar
  • 934
  • 12
  • 27