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)
})
Related
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
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?");
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");
Processing a bunch of files using Node and I need to separate the file name from the directory. Does node have a simple way to do this without additional dependencies? I could use an NPM package like Filename Regex, but thought I'd check if there something available out of the box?
So for example suppose we have src/main/css/file.css. Hoping something like this is possible:
const fs = require('fs');
const path = fs.filePath(String pathAndFileName); //path = src/main/css
const file = fs.fileName(String pathAndFileName); //file = file.css
The utilities for manipulating file paths are in the path module.
https://nodejs.org/api/path.html
const {dirname, basename} = require('path');
const path = dirname(String pathAndFileName);
const file = basename(String pathAndFileName);
I am trying to concatenate a bunch of files inside a folder using node's fs.
The problem is that I get a
Uncaught ReferenceError: fs is not defined
When attempting to use fs.readFileSync with the files array in the code below.
var fs = require('fs');
var path = require('path');
var output = "";
var files = fs.readdirSync('./content');
console.log(files); //fs works here
for(var i = 0; i < files.length; i ++) {
output += fs.readFileSync(path.join('./content', files[i]), 'utf8') + '\n';
}
module.exports = output;
At first I thought it was some kind of scope issue, but then I came across this issues fs is not defined error when readFileSync is passed a path variable which basically says:
You can't use variables since the expression has to be statically analyzable, i.e. Known at build time, not run time.
So is there any other way to go about concatenating those files?
Thanks for any help!