Using ES6 module in node-red

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.

Have you tried dynamic import?


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

(requires Nodejs 13 or greater)

1 Like

Perhaps I have misunderstood you, but can't you do it like I did here? In my case I have defined the class in another npm package.

@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 :wink:

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...

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.

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