ashtuchkin / iconv-lite

Convert character encodings in pure javascript.
MIT License
3.04k stars 282 forks source link

Error - Cannot read property 'isEncoding' of undefined #230

Open Vartex05 opened 4 years ago

Vartex05 commented 4 years ago

Hi, iam trying to use iconv-lite library in my ionic5 app, but when i tried to encode some string i get this error:

core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'isEncoding' of undefined
TypeError: Cannot read property 'isEncoding' of undefined
    at Object../node_modules/string_decoder/lib/string_decoder.js (string_decoder.js:29)
    at __webpack_require__ (bootstrap:84)
    at Object../node_modules/iconv-lite/encodings/internal.js (internal.js:49)
    at __webpack_require__ (bootstrap:84)
    at Object../node_modules/iconv-lite/encodings/index.js (index.js:6)
    at __webpack_require__ (bootstrap:84)
    at Object.getCodec (index.js:65)
    at Object.getEncoder (index.js:117)
    at Object.encode (index.js:22)
    at PrintService.testEncode (print.service.ts:26)
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:862
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)
    at drainMicroTaskQueue (zone-evergreen.js:559)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:469)
    at invokeTask (zone-evergreen.js:1603)
    at HTMLElement.globalZoneAwareCallback (zone-evergreen.js:1629)

This looks like a problem with unknown encoding, but i call it correctly: iconv.encode('test','cp852')

Any ideas why this happens? Iam using ionic 5 with angular 8

ashtuchkin commented 4 years ago

Looks like the exception is raised on this line: https://github.com/nodejs/string_decoder/blob/master/lib/string_decoder.js#L29

It seems that require('safe-buffer').Buffer is undefined in your case. Might be something with your webpack setup so that it doesn't package safe-buffer module? I'm not an expert in ionic nor angular. You might want to ask for help in safe-buffer project, or string_decoder.

Vartex05 commented 4 years ago

Iam trying to get more info, but i was unable to solve it. Odd is, that when i try to run for exampleiconvlite.encodingExists("win1251") it returns false. Maybe i need to install some more npm packages, when iam using iconv-lite on frontend?

ashtuchkin commented 4 years ago

usually you don't need to install anything else. In this particular case you could try to add 'safe-buffer' or 'string_decoder' to you dependencies and try that. I'm not familiar with ionic though, so not sure.

It might also be related to #118 - see if you can try some of the workarounds in that thread.