-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 notutf-8
. Read the error message...– Luiz Felipe
making 'uft8' still returns error. Typeerror [ERR_INVALID_OPT_VALUE_ENCODING]: The value "uft8" is invalid for option "encoding"
– Gabriel Ribeiro Carneiro
If you change the second argument to
{ encoding: 'utf8' }
solves the problem?– Luiz Felipe
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 withutf8
, and it also worked... Which version of your Node?– Luiz Felipe
My version is 'v12.16.1'
– Gabriel Ribeiro Carneiro
Obg @Luiz Felipe for help
– Gabriel Ribeiro Carneiro