I'm trying to build an app using electron, it's designed to get a GUI later but for now i'm just trying to do this:
function test(){
console.log("In Test")
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
console.log(line);
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', test);
On launching npm start, I see the "In Test" log, but when I type something in my shell, it's not returned to me as I need it to be by the Readline module.
Am I missing something ?
In Electron the console becomes the Chromium console which is not like the terminal you're used to. Readline is not going to work as far as I know. The blink console in Chromium does not support reading input this way. It operates more like a debug REPL where you can type JS code, inspect variables, etc. It's not for user input. I don't think you're going to be able to get input supplied from that console into stdin, which is where the readline module is waiting to see data.
Update
I assumed OP was using the dev tools console expecting it to work like a shell. He was using it properly. The actual issue is a bug with node's readline module on Windows. Node devs are actively working to fix it. It's a regression bug that was fixed once before but appeared again in recent versions of node.
Related
I am using vsCode for NodeJS.
My code was simple. So I was expecting stdout should wait for input from console. Once input is given, then it should go to exit.
But once i do > node process
the program goes to exit directly.
file - process.js ->
process.stdout.write('Ask me something : ');
process.stdin.on('data', function(answer){
console.log(answer.toString());
});
console.log('exit')
process.exit();
Output:
c:\node-projects\demo-nodejs>node process
Ask me something : exit
c:\node-projects\demo-nodejs>
How can I use process object to get standard input/output/print/exit the code instead of using other readline object?
There are a few question/answers such as this one already on this site that probably answer what you are asking. Your code is not working because the line with process.stdin.on is not a synchronous callback method call, it is an event handler that waits for the 'data' event.
If I understand what you are trying to do, I think it is easiest to import the 'readline' module. It is built-in to the nodejs executable, but not loaded to your code execution environment unless you require it.
That would make your code look like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Ask me something : ', function(answer){
console.log(answer.toString());
console.log('exit')
process.exit();
});
Note that the rl.question method call executes right away and calls the function(answer) function when the user is done with text input. It can take some understanding and getting used to the asynchronous program control flow in NodeJS, you might have to rethink some of the rest of your program structure.
The offical documentation for the readline NodeJS module and function is here.
I have a text blob that contains ip, port, user and passord and would like to write a small utility script where I can:
paste the text in stdin
extract the connection details using regex or any other means
use node to launch ssh interactively using my parsed values
How would I launch the ssh command from node with the arguments, exit node and continue on ssh'ing? The documentation I've found regarding i.e. child_process concerns launching and controlling the process within node, but I simply want to exit back and take over once ssh is started.
The following code will fit your requirements, adjust it to your needs. The program does the following:
reads the port from a file
prompts you for the hostname, reading data from stdin
launches ssh using child_process.fork
Code:
var port = require('fs').readFileSync('port.txt');
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter hostname: ', (answer) => {
require('child_process').spawn('ssh', [answer, port],
{stdio: [process.stdin, process.stdout, process.stderr]});
});
The question is answered except for the following point:
exit node and continue on ssh'ing
I am not aware of this being possible in any programming language, but a better answer can correct me. Instead, you launch a child process and redirect all input/output to it. From your perspective, you only interact with ssh, so the fact that the node process still exists as a parent to ssh is transparent to you.
An equivalent command in perl for example would be system("ssh");
I want to pass a message from a nodejs script to chrome extension using nativeMessaging and thus am using the chrome-native-messaging node module in the following way to pass a message to stdout.
var rs = new stream.Transform({objectMode: true});
var msg = {"message":"pingfromelectron"};
rs.push(msg);
rs.pipe(output).pipe(process.stdout);
However, this is writing the output twice to stdout as you can see in the screenshot below:
I am not quite sure why this is happening. Any pointers would be helpful.
I'm using require("child_process").spawn and calling an executable, which at the beginning is pausing for a password to be input; if I use it like below:
var spawn = require("child_process").spawn;
var exe = spawn("NameOfExe.exe", [/*arguments*/], {stdio: [process.stdin, process.stdout, process.stderr]});
It works perfectly (Yes, I know I can do inherit, shush :p ). I can type inside my console and it'll accept it with no issues.
However, if I do it like this:
var spawn = require("child_process").spawn;
var exe = spawn("NameOfExe.exe", [/*arguments*/], {stdio: ["pipe", process.stdout, process.stderr]});
exe.stdin.write("Password\n");
then the executable doesn't even receive the stdin; it immediately goes to Execution Failed.
I'm completely at a loss for this. Any suggestions?
EDIT:
I think I may be onto something!
So I'm 99.99% certain that the executable is using C# and Console.ReadKey to get the password. However, according to Microsoft, an exception gets thrown whenever the In property is redirected somewhere else.
So this explains it, but is there a way around this using Node, or am I at the mercy of the people who made this executable?
The ReadKey method reads from the keyboard even if the standard input is redirected to a file with the SetIn method.
You figured it out. It uses native bindings to the hardware controller/HAL, and not the shell, to handle stdio.
One of the pleasures of frameworks like Rails is being able to interact with models on the command line. Being very new to node.js, I often find myself pasting chunks of app code into the REPL to play with objects. It's dirty.
Is there a magic bullet that more experienced node developers use to get access to their app specific stuff from within the node prompt? Would a solution be to package up the whole app, or parts of the app, into modules to be require()d? I'm still living in one-big-ol'-file land, so pulling everything out is, while inevitable, a little daunting.
Thanks in advance for any helpful hints you can offer!
One-big-ol'-file land is actually a good place to be in for what you want to do. Nodejs can also require it's REPL in the code itself, which will save you copy and pasting.
Here is a simple example from one of my projects. Near the top of your file do something similar to this:
function _cb() {
console.log(arguments)
}
var repl = require("repl");
var context = repl.start("$ ").context;
context.cb = _cb;
Now just add to the context throughout your code. The _cb is a dummy callback to play with function calls that require one (and see what they'll return).
Seems like the REPL API has changed quite a bit, this code works for me:
var replServer = repl.start({
prompt: "node > ",
input: process.stdin,
output: process.stdout,
useGlobal: true
});
replServer.on('exit', function() {
console.log("REPL DONE");
});
You can also take a look at this answer https://stackoverflow.com/a/27536499/1936097. This code will automatically load a REPL if the file is run directly from node AND add all your declared methods and variables to the context automatically.