# Manage import module in function

**URL:** https://discourse.nodered.org/t/manage-import-module-in-function/37010
**Category:** General
**Created:** [4 December 2020 15:19 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010 "2020-12-04T15:19:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![NoSuchException](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@NoSuchException](https://discourse.nodered.org/u/NoSuchException)
#### Post date: [4 December 2020 15:19 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/1 "2020-12-04T15:19:49Z")

</div>

Hi all,  
I'm making a function to parse the msg files of outlook because node-red-contrib-mail-parse works only with eml file.  
I'm using the module @kenjiuno/msgreader so I installed the module with npm in the node-red folder and I wrote in the settings.js the row msgreader:require('@kenjiuno/msgreader') in functionGlobalContext and I import it in my function node with const MsgReader = global.get('msgreader');

In a normal script this module can be imported with import MsgReader from '@kenjiuno/msgreader' and then it need to create the object with const testMsg = new MsgReader(msgFileBuffer).  
In function Node I can't use import so I use const MsgReader = global.get('msgreader'); but the function try the error "TypeError: MsgReader is not a constructor" at the row const testMsg = new MsgReader(msgFileBuffer).

MsgReader is an export of the module @kenjiuno/msgreader so I modified const MsgReader = global.get('msgreader'); in const MsgReader = global.get('msgreader').MsgReader but I have the same error;  
I think the problem is that the contruct import-from is not correctly interpreted by the global.get('msgreader') but I didn't understant how I can to translate import MsgReader from '@kenjiuno/msgreader' for node function of nodered.  
Can you help me?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 December 2020 17:48 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/2 "2020-12-04T17:48:27Z")

</div>

> [@NoSuchException](#):
>
> @kenjiuno/msgreader

When facing problems like this, best to raise an issue in the authors github:

[Usage with node.js · Issue #5 · HiraokaHyperTools/msgreader (github.com)](https://github.com/HiraokaHyperTools/msgreader/issues/5)

---

<div class="post-metadata">

### Author: ![NoSuchException](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@NoSuchException](https://discourse.nodered.org/u/NoSuchException)
#### Post date: [9 December 2020 10:25 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/3 "2020-12-09T10:25:17Z")

</div>

Great,  
with the suggested workaround from the author it works good.

Thanks.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [9 December 2020 10:34 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/4 "2020-12-09T10:34:03Z")

</div>

Can you please share so that other people know what the resolution was?

---

<div class="post-metadata">

### Author: ![NoSuchException](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@NoSuchException](https://discourse.nodered.org/u/NoSuchException)
#### Post date: [9 December 2020 11:14 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/5 "2020-12-09T11:14:29Z")

</div>

Sure, my function is

```javascript
const MsgReaderOrExports = global.get('msgreader');
const MsgReader = MsgReaderOrExports.default || MsgReaderOrExports;

const testMsg = new MsgReader(msg.payload);
const testMsgInfo = testMsg.getFileData();

msg.payload = testMsgInfo;
return msg;

```

in the row `const testMsg = new MsgReader(msg.payload);` msg.payload is the binary of the file that I extract with other function by the file received with a POST call

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [23 December 2020 11:14 UTC](https://discourse.nodered.org/t/manage-import-module-in-function/37010/6 "2020-12-23T11:14:32Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
