nyhunter77 nyhunter77 - 1 year ago 98
Javascript Question

In Node.js, asking for a value using Prompt, and using that value in a main js file

I'm pretty new to and it seems fairly easy to use but when it comes to getting a value using the command line and returning that value to be used in another package or

, it seems harder than I expected.

Long story short, I've used a package (
), to enter a file to purge on the network successfully.

I want to make it more dynamic though by prompting the user to enter the file they want purged and then using that in the package.

After making a few tries using
var stdin = process.openStdin();
I actually found another npm package called
that seemed to be easier. Both ways seem to have the same problem though.

Node doesn't seem to want to stop for the input. It seems to want to automatically make the purge without waiting for input even though I've called that module first. It actually gets to the point where I should enter the file but it doesn't wait.

I am definitely missing something in my understanding or usage here, what am I doing wrong?

My code so far is:

var purgeUrl = require('./getUrl2');

var PurgerFactory = require('../../node_modules/akamai-ccu-purge/index'); // this is the directory where the index.js of the node module was installed

// area where I placed the authentication tokens
var config = {
clientToken: //my tokens and secrets akamai requires

// area where urls are placed. More than one can be listed with comma separated values
var objects = [
purgeUrl // I am trying to pull this from the getUrl2 module

// Go for it!
var Purger = PurgerFactory.create(config);

Purger.purgeObjects(objects, function(err, res) {
console.log('Purge Result:', res.body);
Purger.checkPurgeStatus(res.body.progressUri, function(err, res) {
console.log('Purge Status', res.body);
Purger.checkQueueLength(function(err, res) {
console.log('Queue Length', res.body);

module looks like this:

var prompt = require('../../node_modules/prompt');
// Start the prompt

// Get property from the user
prompt.get(['newUrl'], function (err, result) {
// Log the results.
console.log('Command-line input received:');
console.log(' http://example.com/custom/' + result.newUrl);
var purgeUrl = 'http://example.com/custom/' + result.newUrl;
module.exports = purgeUrl;

Thanks again for the help!

Answer Source

I would probably just allow getURL2 to expose a method that will be invoked in the main module. For example:

// getURL2

var prompt = require('../../node_modules/prompt'); 

module.exports = {
  start: function(callback) {


    prompt.get(['newUrl'], function (err, result) {
      // the callback is defined in your main module
      return callback('http://example.com/custom/' + result.newUrl);


Then in your main module:

require('./getUrl2').start(function(purgeURL) {
  // do stuff with the purgeURL defined in the other module

The implementation may differ, but conceptually, you need to make your second module, which requires some sort of input from the user, happen as a result of that input. Callbacks are a common way to do this (as are Promises). However, as prompt is not necessarily exposing a method that would necessitate a Promise, you can do it with plain old callbacks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download