Treat File as String in Nodejs

Asked

Viewed 217 times

-1

I am having the following problem : I wish to carry out the count of line breaks, ie the amount of '\n' in a file. For this, I wish while reading the file to treat it as a String. I am trying it as follows :

    const fs = require('fs') 

    const contents = fs.readFileSync('<caminho para o arquivo>', 'uft-8')//permitir ler o arquivo desejado

    const matches = contents.match(/\n/g)//identifica o '\n' no arquivo

    const count = matches.length//indica a qtd de '\n' no arquivo

    console.log(count)

I’m getting the following error message :

**internal/fs/utils.js:85
    throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding);
    ^
TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "uft-8" is invalid for option "encoding"
    at assertEncoding (internal/fs/utils.js:85:11)
    at getOptions (internal/fs/utils.js:221:5)
    at Object.readFileSync (fs.js:357:13)
    at Object.<anonymous> (/home/gabriel_rc/Área de Trabalho/Curso de NodeJS/exercícios/3.js:10:21)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_INVALID_OPT_VALUE_ENCODING'
}**

How to solve this?

  • The correct is utf8, and not utf-8. Read the error message...

  • making 'uft8' still returns error. Typeerror [ERR_INVALID_OPT_VALUE_ENCODING]: The value "uft8" is invalid for option "encoding"

  • If you change the second argument to { encoding: 'utf8' } solves the problem?

  • The funny thing is it was supposed to work even with utf-8 (what the error message accuses of being wrong). See here. I also tested with utf8, and it also worked... Which version of your Node?

  • My version is 'v12.16.1'

  • 1

    Obg @Luiz Felipe for help

Show 1 more comment

1 answer

2


Just like @Luiz Felipe quoted in the comments, you put the wrong encoding, but you did it twice.

The first time as 'uft-8' as per your example:

TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "uft-8" is invalid for option "encoding"

And the second, as 'uft8' according to the error you put in the comments:

TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "uft8" is invalid for option "encoding"

Both times you reversed the acronym, the prefix is utf and both times you wrote uft.

Therefore, the main fix in your code is:

const contents = fs.readFileSync('myfile.txt', 'utf8');//permitir ler o arquivo desejado

Your code would then look like this:

const fs = require('fs');

const contents = fs.readFileSync('myfile.txt', 'utf8');//permitir ler o arquivo desejado

const matches = contents.match(/\n/g);//identifica o '\n' no arquivo

const count = matches.length;//indica a qtd de '\n' no arquivo

console.log(count);

It is worth mentioning the possibility of exception if the file does not exist and if it does not have a line break, currently the code works in the certainty of the existence of the file and in the certainty that there is at least a line break with \n.

See this example online: https://repl.it/repls/UnitedEsteemedRuntimeenvironment

Documentation: https://nodejs.org/api/fs.html

  • 1

    I’m already so hung up on utf I didn’t even realize I’d been placed uft. What an eye! D

  • Yeah... It was also hard to understand why it hadn’t worked! : D

  • Obg @Danielmendes, It worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.