Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError [ERR_INVALID_OPT_VALUE_ENCODING] The value "./ab.txt" is invalid for option "encoding"

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"

like image 469
MAK Avatar asked Oct 03 '18 07:10

MAK


1 Answers

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.

like image 64
Sam-Sm Avatar answered Nov 01 '22 03:11

Sam-Sm