read input from console without node.js - javascript

Hey I was wondering if there's a js equivalent to cin from c++. I tried using readLine() and prompt as suggested on the internet but I have been thrown Undefined Reference for both functions. Later I found out that readLine and prompt both run on Node.js or smth but I was wondering if there's a command I can use to run smth that's just pure HelloWorld.js and stuff.
(I haven't learned Node.js so correct me if I'm misunderstanding this)
Thanks.
Here's some of my code if it helps at all:
theInput = readline();
I have tried:
const readLine = require('readline');
const theInput = readline.createInterface({
input: process.stdin,
output: process.stdout
});
and
const theInput = prompt("What's your name?");

Related

How to take user input in Node.js - ESM not CJS

I am doing a project where I am supposed to create a JavaScript RPG CLI game using Node.js. I am supposed to use ESM and not CommonJS. Once executed, the script has to output Welcome with a menu underneath where you choose to either start the game, load or exit the game. Then, it demands user input to choose an option.
I have put type: 'module' in my package.json to use ESM. I tried with readline and with inquirer.js but nothing works. I installed inquirer.js with npm i inquirer, imported it but it doesn't work. I literally just started this project, I basically have but few lines of the code. I don't know where the problem is.
Here is one of the codes that I tried:
import readline from "readline";
import { stdin as input, stdout as output } from "node:process";
const run = (args) => {
console.clear();
console.log(`
+-----------------------------+
| Welcome ! |
+-----------------------------+
`);
console.log(`
1. Start game 👍
2. Load game 💾
3. Exit ❌
`);
const rl = readline.createInterface({ input, output });
rl.question("Your choice (1-3): ");
rl.close();
};
export default run;
Does this help?
import readline from "readline";
import { stdin as input, stdout as output } from "node:process";
const run = (args) => {
console.clear();
console.log(`
+-----------------------------+
| Welcome ! |
+-----------------------------+
`);
console.log(`
1. Start game 👍
2. Load game 💾
3. Exit ❌
`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Your choice (1-3): ", (in)=>{
//your code here
// example:
switch () {
case 1:
start();
break();
case 2:
load();
break();
case 3:
exit();
break();
}
rl.close()
});
};
export default run;
Of course you need to create start(), load() and exit() functions

How do I read a file and change every line in it?

I need to read a multiline txt file and do some manipulations with each line.
I'm trying to use readline but I can't figure out how to use the lines it reads.
Here I use the most standard readline code:
const readline = require ('readline')
const fs = require ('fs')
const rl = readline.createInterface({
input: fs.createReadStream('price.txt'),
})
rl.on('line', function(line){
console.log(line)
})

html-minifier node.js TypeError: value.replace is not a function

I am trying to minify html file using the node module html-minifier. In order to this I have created this little node.js file which should be able to do this
'use strict'
var fs = require('fs');
var minifier = require('html-minifier').minify;
var htmlFile = fs.readFileSync("users/email/test.html");
var output = minifier(htmlFile, {
removeAttributeQuotes: true
});
process.stdout.write(output);
but when I run the program I get the following error.
TypeError: value.replace is not a function
Any idea why this is happening. I am using version 4.0.0 of html-minifier
Since you haven't specified a text encoding, readFileSync returned a Buffer, not a string. See the readFileSync documentation.
If you know the encoding to use, you can specify it as a second argument:
var htmlFile = fs.readFileSync("users/email/test.html", "utf8");

Duplicate lexical declaration; cannot redefine let or const variables

I am using VS 2015 Community for a Node application using nodejstools tool.
Using the following code I get an error in "Error List" palette. Despite this, the application runs fine.
I am wondering why this error happens in my code or it is a bug related to VS IDE.
'use strict';
const Q = require('q')
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const fs = require('fs');
const del = require('del');
const removeDirectories = require('remove-empty-directories');
Edit:
Source code for this exception.
Bug report

Reload modules on the fly in Node REPL

I am testing my module with REPL like this:
repl.start({
input: process.stdin,
output: process.stdout
})
.context.MyModule = MyModule;
Is there a way to reload the module automatically, when I change and save it, without having to exit and run repl again?
You can use the chokidar module and force reload (you will lose runtime context in the module, but it should auto-reload).
var ctx = repl.start({
input: process.stdin,
output: process.stdout
})
.context;
ctx.MyModule = require('./mymodule');
chokidar.watch('.', {ignored: /[\/\\]\./}).on('all', function(event, path) {
delete require.cache['./mymodule'];
ctx.MyModule = require('./mymodule');
});
If that doesn't work, I'm happy to play with it a little and get a working solution.
edit: if it doesn't garbage-collect cleanly (there are any open handles/listeners), this will leak each time it reloads. You may need to add a 'clean-exit' function to MyModule to stop everything gracefully, and then call that inside the watch handler.

Categories

Resources