const fs=require('fs');
var read = fs.createReadStream(__dirname,'./ab.txt','utf8');
read.on('data',function(chunk){
console.log("New Chunk Received ");
console.log(chunk);
});
I am using fs module in the express app and trying to read the text file but command prompt is giving me the below error.
internal/fs/utils.js:41 throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding); ^
TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "./ab.txt" is invalid for option "encoding"
Looks like the problem here is the fs function params
You are passing './ab.txt' as the 2nd parameter, which is taking that as the encoding.
This should work:
const fs = require('fs');
const path = require('path');
const read = fs.createReadStream(path.join(__dirname,'./ab.txt'));
read.on('data',function(chunk){
console.log("New Chunk Received");
console.log(chunk);
});
'utf-8' is the standard encoding that fs goes for, so no need to pass this either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With