# Using ES6 module in node-red

**URL:** <https://discourse.nodered.org/t/using-es6-module-in-node-red/45906>\
**Category:** General\
**Created:** [19 May 2021 10:56 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906 "2021-05-19T10:56:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![terchris](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terchris/32/42023_2.png) [@terchris](https://discourse.nodered.org/u/terchris)\
**Post date:** [19 May 2021 10:56 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/1 "2021-05-19T10:56:32Z")

</div>

I have created a program that creates company profiles by extracting data from several rest api's, web scraping and so on. I Just came across node-red and I wanted to use it to visualize the steps that my code does when data flows through and gets enriched.

The code is written as an ES6 module and I use: `import { myfunction} from "mymodule";` I also use the module in the nextjs part of my system so I can't just rewrite it to javascript.

I'd like to use functions from my library/module in node-red. Tried to set `"type": "module"` in package.json But that did not work. There must be some best practices to do this. I find it hard to believe that es6 modules cannot be used with node-red.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 May 2021 11:15 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/2 "2021-05-19T11:15:46Z")

</div>

Have you tried [dynamic import](https://nodejs.org/api/esm.html#esm_import_expressions)?

e.g.

```auto
let module = await import('/modules/my-module.js');

```

(requires Nodejs 13 or greater)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [19 May 2021 15:26 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/3 "2021-05-19T15:26:23Z")

</div>

Perhaps I have misunderstood you, but can't you do it like I did [here](https://github.com/bartbutenaers/node-red-contrib-msg-speed/blob/master/msg_speed.js#L29)? In my case I have defined the class in [another](https://github.com/bartbutenaers/nr-msg-statistics/blob/main/msg_analyzer.js#L19) npm package.  
Bart

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 May 2021 15:53 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/4 "2021-05-19T15:53:36Z")

</div>

@BartButenaers can you fix your `here` link - it isn't going anywhere at the moment and we're curious to see what you're linking to 😉

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [19 May 2021 15:58 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/5 "2021-05-19T15:58:02Z")

</div>

I have fixed the link. But I think I have misinterpreted the question, and therefore I have a feeling you will be very disappointed after opening my link...

---

<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:** [19 May 2021 16:47 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/6 "2021-05-19T16:47:31Z")

</div>

I personally find this a really annoying aspect of Node.js in that it doesn't behave the same way as using ECMAScript modules in a browser.

I think you have a number of options but I'm not sure that all of them will play nicely with Node-RED as I've not tried.

- rename your module to .mjs instead of .js and Node will automatically treat it as an ECMAScript module.
- use your package.json to mark your code as a module - the "type" property (which you've already tried)
- use a build step to translate - this is most likely to be the most compatible option.

Really, it looks like you need to be using Node.js v12.20 or newer if working with ECMAScript modules.

---

<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:** [18 July 2021 16:48 UTC](https://discourse.nodered.org/t/using-es6-module-in-node-red/45906/7 "2021-07-18T16:48:27Z")

</div>

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