简体   繁体   中英

Using the prompt package in node.js

I've recently learned the basics to using javascript through Node.js and have got a few questions as to how to use the prompt npm package to prompt the user for input. The url for the package is: https://npmjs.org/package/prompt

If someone could explain how to use the prompt package without complicating the details too much that would be helpful as in the browser it is as simple as typing: prompt('Your prompt here') .

Thanks

There's a very simple example right in the documentation for the prompt package -- though I think the docs are actually out-of-date and that example didn't work for me.

Here is an ultra-simple example. Note that you have to pass a callback to the prompt function, instead of receiving a return value from it, because that's just the way life is when you are using node.js ;)

var prompt = require("prompt");

prompt.start();

prompt.get("name", function(err, result) {
    console.log("Your name is ",result.name)
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM