Skip to content

Add web APIs for alert, confirm and prompt to Node.js #174

@iansu

Description

@iansu

Deno supports the alert, confirm and prompt web APIs to read input from the user in a terminal context. I think this would be great to have in Node.js as well. Deno documentation is here: https://examples.deno.land/prompts

You can achieve the same thing in Node with readline but it's not as nice of an API. For example, here's how you prompt the user for input using readline from the Node docs (https://nodejs.org/api/readline.html#readline):

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

const rl = readline.createInterface({ input, output });

rl.question('What do you think of Node.js? ', (answer) => {
  // TODO: Log the answer in a database
  console.log(`Thank you for your valuable feedback: ${answer}`);

  rl.close();
});

And here's how you would do the same thing with prompt:

const answer = prompt('What do you think of Node.js?');

console.log(`Thank you for your valuable feedback: ${answer}`);

I think these new APIs would be very useful for tool authors and would also bring more web APIs to Node.js.

One possible difficulty implementing these APIs is that they seem to be synchronous in Deno and readline does not work synchronously. I've created a basic POC of these functions using readline (and await) as a starting point here: https://github.com/iansu/node-prompts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions