# Tip: load an ES module in the js file of a custom node

**URL:** https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510
**Category:** Developing Nodes
**Created:** [26 April 2024 08:05 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510 "2024-04-26T08:05:37Z")
**Posts on this page:** 7
**Page:** 1

<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: [26 April 2024 08:05 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/1 "2024-04-26T08:05:38Z")

</div>

Hi folks,

Just sharing something I found. Note that I have no clue at the moment whether this works for all libraries. Don't have enough free time to figure out stuff like that. So if anybody else knows more about the topic or knows a better solution, please share your knowledge.

I needed to use the [svglint](https://www.npmjs.com/package/svglint) npm package in the backend js file of one of my custom nodes. However that library is an ES (EcmaScript) module and no classic CommonJs module.

- When I import the _ **ES module** _ in my js file:

- When I load it like a classic _ **CommonJs module** _:

- So after some trial and error I managed to do a _ **dynamic import** _, like suggested in the previous error message:

Hopefully this can be of any help to other developers...

Bart

---

<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: [26 April 2024 10:39 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/2 "2024-04-26T10:39:17Z")

</div>

Thanks Bart, not sure if you saw my Lounge post the other day about this.

Node-RED actually already uses this approach in core.

The big issue is that dynamic imports are async. That can often mean that you need to reorganise your code to be async back up a chain of function calls. That can require very extensive changes to existing code.

But yes, when you can cope with that issue, it absolutely works.

For the future, there is now an experimental feature in the new Node.js v22 that will allow synchronous requires of ESM's - long overdue.

There are some serious gotya's when working with CJS modules in ESM's as well - mostly around how different `import` is from `require()`.

Safe to say that the move from CJS to ESM (which does need to happen since ESM is now the JavaScript standard and works in the browser as well) is a MESS in Node.js. ☹

* * *

What I'd like to eventually do is create a test Node that uses ESM for libraries and dependencies to see what impact that might have on the top-level code.

I've already a lot of async code now in my nodes due to the handling of Typed Inputs so possibly now less of an issue at least for new nodes. Certainly I don't seem to have any issues making the `input` code async.

---

<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: [26 April 2024 11:38 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/3 "2024-04-26T11:38:02Z")

</div>

> [@TotallyInformation](#):
>
> not sure if you saw my Lounge post the other day about this.

Not really... Do you mean the Discourse Lounge? Long time ago that I received that. Must have been kicked out of it in that case...

> [@TotallyInformation](#):
>
> Node-RED actually already uses this approach in core

Ah I wasn't aware of that. Didn't have a look in the core for this one...  
I know that this has been discussed it a number of times in the past, but wasn't clear anymore to me if someone had shared a solution/workaround already.

> [@TotallyInformation](#):
>
> there is now an experimental feature in the new Node.js v22 that will allow synchronous requires of ESM's - long overdue.

Mind blowing that they didn't provide something like that from day one.  
Like you say it has become a complete mess.  
When a lib switches from CommonJs to ES, I see advices all over the place to keep using the last unmaintained CommonJs version. Not really a solution for the problem...

Thanks for the feedback!

---

<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: [26 April 2024 13:28 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/4 "2024-04-26T13:28:31Z")

</div>

> [@BartButenaers](#):
>
> When a lib switches from CommonJs to ES, I see advices all over the place to keep using the last unmaintained CommonJs version. Not really a solution for the problem...

Indeed. I had/have a couple of those in uibuilder dependencies. Though now I'm preparing v7, I've already managed to get rid of them.

The longer I deliver in this space, the more and more determined I get to avoid dependencies and frameworks. I'm down to just 7 dependencies now (and no frameworks of course!) of which one is my module anyway. `fs-extra` is my next target and I'm moving all filing system access into a single library and eliminating `fs-extra` calls along the way.

---

<div class="post-metadata">

### Author: ![Stwissel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stwissel/32/126_2.png) [@Stwissel](https://discourse.nodered.org/u/Stwissel)
#### Post date: [11 June 2024 02:05 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/5 "2024-06-11T02:05:22Z")

</div>

Do we have a guide for Node creation using ESM?

Ran into "only ESM forward" with `chai` for testing and a few libraries I looked at for new nodes.  
I naively tried `type=module` in package.json, swapped r`equire` for `import`, but then NodeRED wouldn't load the node.

What do I miss?

---

<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: [11 June 2024 11:33 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/6 "2024-06-11T11:33:23Z")

</div>

> [@Stwissel](#):
>
> Do we have a guide for Node creation using ESM?

No, I don't believe so.

> [@Stwissel](#):
>
> What do I miss?

Without knowing the code, hard to say. I do know that Node-RED's module loaded does allow for ESM but it has to load modules asynchronously and I don't know if that will cause any timing edge-cases (I assume we are talking about the runtime code here, in the Editor code, use of a module is probably a bit simpler assuming you limit yourself to browsers that support ESM, which shouldn't be hard now).

There are also a few things in Node.js that work slightly differently when switching to ESM from CommonJS - the Node.js docs have a decent description of what doesn't work.

---

<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: [10 August 2024 11:34 UTC](https://discourse.nodered.org/t/tip-load-an-es-module-in-the-js-file-of-a-custom-node/87510/7 "2024-08-10T11:34:01Z")

</div>

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