HiraokaHyperTools / msgreader

35 stars 9 forks source link

Usage with node.js #5

Closed curldapps closed 4 years ago

curldapps commented 4 years ago

Hi - do you have any advice for using with node.js where import-as is not viable? I am locked into an older version of node.

UPDATE: Even using Node.js 14+ with import/export enabled I get "MsgReader" is not a constructor when I'm trying to make a new instance of the class in my app. Same goes for importing using require() in older versions of node : const msgreader = require("@kenjiuno/msgreader")

kenjiuno commented 4 years ago

@curldapps Hi thanks for informing this issue! I knew this unexpected behavior of Node.js 14 today.

Here is workaround I found a while ago:

import MsgReaderOrExports from '@kenjiuno/msgreader'

const MsgReader = MsgReaderOrExports.default || MsgReaderOrExports;

console.log(new MsgReader());

I'll continue to find a method to fix this problem!

curldapps commented 4 years ago

Thanks so much, @kenjiuno!

Do you by chance have a little trick or workaround for Node 10 -> 12? :) I'd love to be able to import and use this using the require() syntax within my project natively. If not, I'll spin up a little containerized endpoint with Typescript and Node 14.

Cheers!

kenjiuno commented 4 years ago

Ok, this will work on Node.js v12.17.0:

const MsgReaderOrExports = require('@kenjiuno/msgreader')

const MsgReader = MsgReaderOrExports.default || MsgReaderOrExports;

console.log(new MsgReader());
C:\A\t>node index2
MsgReader {
  ds: DataStream {
    _byteOffset: 0,
    position: 0,
    endianness: true,
    _buffer: ArrayBuffer { [Uint8Contents]: <00>, byteLength: 1 },
    _dataView: DataView { byteLength: 1, byteOffset: 0, buffer: [ArrayBuffer] },
    _dynamicSize: true,
    _byteLength: 1,
    failurePosition: 0
  },
  fileData: undefined
}
curldapps commented 4 years ago

I really appreciate it!

curldapps commented 4 years ago

Solved using import methodology @kenjiuno suggested in Node 10