3

I'd like to know how to run this script without entering node script_name.js into the terminal. The prompt is displayed when I start the script from the terminal, but when I try to run the script as a program (from the file manager, not the command line), it simply opens and closes the terminal, without displaying a prompt.

#!/usr/local/bin/node

var prompt = require('prompt');

//var stuff = require("./stuff");

  prompt.start();

  prompt.get(['username', 'email'], function (err, result) {
    if (err) { return onErr(err); }
    console.log('Command-line input received:');
    console.log('  Username: ' + result.username);
    console.log('  Email: ' + result.email);
  });

  function onErr(err) {
    console.log(err);
    return 1;
  }

Here is the expected output (with input) for this program:

anderson@anderson-Ideapad-Z560:~/AeroFS/node.js examples$ node node_prompt_demo.js
prompt: username:  blah
prompt: email:  blah@example.com
Command-line input received:
  Username: blah
  Email: blah@example.com
Anderson Green
  • 25,996
  • 59
  • 164
  • 297
  • The script runs perfectly when I launch it from the terminal, but it doesn't work when I select "Run in Terminal" from the desktop or file manager. I still don't understand why I can't work with command-line prompts when selecting "Run in Terminal". – Anderson Green Sep 17 '12 at 02:05
  • Also, this question may be relevant (although I think it's for bash scripts, and not node.js): http://askubuntu.com/questions/20330/how-to-run-script-and-dont-close-a-terminal – Anderson Green Sep 17 '12 at 02:07
  • To clarify, I have already set permissions for the script to run as an application. It displays the correct output and gives the correct prompts when I start the script using "node name_of_script.js", but the terminal merely opens and closes when I try to run the script by clicking "Run in Terminal". – Anderson Green Sep 17 '12 at 02:10
  • Here's a related question (but it's certainly not a duplicate): http://stackoverflow.com/questions/4806571/is-it-possible-to-run-node-js-scripts-without-invoking-node?lq=1 – Anderson Green Sep 17 '12 at 02:18
  • it probably has something to do with not attaching to the terminal you're runnning it IN but rather where you ran it FROM. Just a guess though – EhevuTov Sep 19 '12 at 20:33
  • @EhevuTov As a work-around, I've settled on launching node.js scripts from Geany using `node %f`. – Anderson Green Sep 23 '12 at 16:57

1 Answers1

0

This almost certainly has to do with the controlling tty. I'm guessing the file manager you're using doesn't give you a stdin that's attached to anything and it just exits without any input.

Your best bet would be to wrap this in a program that would give it a valid stdin. For example, on a Mac, I run the node script within Terminal.app so it always gets a valid stdin. Should be able to do something similar in XWindows. If you're not using a window manager, you could also try using "expect" and 'screen'. The combination of these should get you what you want.

MarcWan
  • 2,873
  • 3
  • 25
  • 40