mathiasbynens / utf8.js

A robust JavaScript implementation of a UTF-8 encoder/decoder, as defined by the Encoding Standard.
https://git.io/utf8js
MIT License
556 stars 115 forks source link

string.charCodeAt is not a function #26

Open bash0312 opened 7 years ago

bash0312 commented 7 years ago
TypeError: string.charCodeAt is not a function
    at ucs2decode (C:\Users\bash\NEMid\node_modules\utf8\utf8.js:30:19)
    at Object.utf8decode [as decode] (C:\Users\bash\NEMid\node_modules\utf8\utf8.js:201:15)
    at Object.<anonymous> (C:\Users\bash\NEMid\client.js:21:95)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:382:7)

node v7.0.0

mathiasbynens commented 7 years ago

Are you passing undefined to utf8.decode()? If not, how can I reproduce this issue?

hellais commented 7 years ago

I also encountered this error and was at first a bit confused, but then realised that it was happening because I was passing a Buffer instead of a "binary string". Maybe the docs can be made more explicit about the fact that Buffer are actually not supported at least not until https://github.com/mathiasbynens/utf8.js/issues/7 is solved.

jmaister commented 6 years ago

Reading the file with encoding set fixes this problem:

const input = fs.readFileSync('./thefile.txt', {encoding: 'utf8'});
hellais commented 6 years ago

Reading the file with encoding set fixes this problem: const input = fs.readFileSync('./thefile.txt', {encoding: 'utf8'});

If you are reading the file in that way, then you do not need this library.