NodeJS REPL double echoing input, making it hard to use - node.js

When I use the NodeJS repl on linux, everything I type is echoed twice. If I start it up like this:
$ node
Welcome to Node.js v16.11.0.
Type ".help" for more information.
> let repl;
undefined
> import("repl").then(module => { repl = module });
Promise {
<pending>,
[Symbol(async_id_symbol)]: 294,
[Symbol(trigger_async_id_symbol)]: 283,
[Symbol(destroyed)]: { destroyed: false }
}
> let f = repl.start();
> undefined
>
Now everything that I type is doubled up. If I type the letter d, it shows dd. This is preventing me from creating my own REPL setup script.
What am I doing wrong here?

When you start node.js in a terminal without passing a javascript file, by default node actually creates a REPL instance behind the scenes to give you something to interact with in your terminal. So by importing and starting another REPL you now have two REPL instances reading stdin and so you get twice the echo of stdin to stdout.
You can access the default running REPL by running this.repl in a newly opened node terminal. If you want only your own REPL to run, I would recommend declaring your REPL in a javascript file and then executing that file with the node runtime instead.

Related

Interract with a .exe program with js instead of typing in the program

Hello I'm tryna make a Skyrim server Dashboard.
The server look like this =>
On this server i can type some command like this =>
when I manualy wrote /help and it show the output.
I tried to run the executable in node js, the server is working, I can join it, And I can see the output on my VSCode Terminal
But I can't input some text or command
Hope you can help me thanks in advance.
##Its my first ask
Add shell: true to the spawn method so you can still pass commands to the process.
const child = require('child_process').spawn("C:/SkyrimTogetherServer.exe", {
shell: true
});

Getting Unprompted Input in NodeJS

I'm trying to get unprompted input from the user in a NodeJS server. The reason is that I want to be able to send strings that can be interpreted as commands while a server is running such as commands to flush the cache.
Now, originally, I used readline as follows:
import * as ReadLine from 'readline';
export default function InitTerminal() {
const terminal = ReadLine.createInterface({
input: process.stdin,
terminal: false
});
terminal.on('line', (input) => {
console.log(input);
switch(input.toString()) {
default:
console.log('Unknown Command');
break;
}
});
}
The issue was that after most inputs, I would get:
rl.on line * input *
Where * input * refers to the string representation of the buffer input from process.stdin. The weird thing is, the on line triggers only occassionaly and typically I will get that line and my listener doesn't trigger. The same thing happens when I don't use ReadLine and just add a listener to process.stdin. Something is triggering node to print out the output above. I'm using node 11.12.0 and ts-node-dev for typescript in development mode.
So what I'm asking is how can I use node to get unprompted input as commands? Either by modifying my original code or any way I haven't thought of.
Kind thanks.
The problem was that ts-node-dev plays with the stdin. That means it's hard to get the stdin while using it. What I hade to do was create an npm script called build:dev which compiled my typescript and then ran it. Then I could use the process.stdin without concern.
A way to possible watch changes while doing this is to use nodemon instead of ts-node-dev and just compile the typescript when needed

How to change the repl prompt of node.js?

node.js's default repl prompt is >, I sometimes type shell commands into it. In order to easily distinguish it from other programming language repl's and shell prompts, I would like to change it to something like node >. Is it possible, or is there any better practice on this problem?
Create a small node program which fires up a REPL and specifies prompt: 'node >' in the options hash.
See docs here.
// repl.js
const repl = require('repl');
var replServer = repl.start({prompt: 'node > '});
$ node repl.js
node >

node.exe does not read file, but returns ellipsis (...)

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?
You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.
It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Command line for running server

I have a Node server which I would like to debug. Once I've started the server
node server.js
I want to execute functions defined in server.js from a command line. The usual Node REPL "blocks" after the server has started.
For example, if server.js defines the function addBlogPost I want to locally call addBlogPost() and observe changes in the database without passing through a GUI.
Is there an easy way to do this?
You can use the repl module to create a new REPL instance:
repl = require("repl")
r = repl.start("node> ")
r.context.pause = pauseHTTP;
r.context.resume = resumeHTTP;
Now inside the REPL you can use pause() to call pauseHTTP() and resume() to call resumeHTTP().

Resources