Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason for node module @kenjiuno/msgreader error: MsgReader is not a constructor

I'm unable to get even the first lines of the example code from the relatively popular @kenjiuno/msgreader for parsing Outlook .msg files to work. I've installed the module with npm successfully, and my code is:

const fs = require('fs')
const MsgReader = require('@kenjiuno/msgreader')

const msgFileBuffer = fs.readFileSync('./test-email.msg')
const testMsg = new MsgReader(msgFileBuffer)

But I get the error: "MsgReader is not a constructor".

A quick console log of MsgReader returns { default: [Function: MsgReader] }. I also tried doing it as a function (no 'new' keyword) which also produced an error.

The only difference between my code and the example code is that they use import (import MsgReader from '@kenjiuno/msgreader') whereas I've used require, but presumably that couldn't make a difference?

Any ideas anyone?

like image 589
Ant Avatar asked Jul 21 '26 14:07

Ant


1 Answers

I ended up changing the require statement to add ["default"] which fixed the issue:

const MsgReader = require('@kenjiuno/msgreader')["default"]

I looked at the library code and made a guess based on the export statement using that 'default' syntax. Is this issue something to do with commonJS or something? If anyone can explain this to me that would be great!

like image 82
Ant Avatar answered Jul 24 '26 04:07

Ant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!