Skip to content

Add support for installing ESM module nodes#4355

Open
hlovdal wants to merge 6 commits intonode-red:devfrom
hlovdal:installing_esm_nodes
Open

Add support for installing ESM module nodes#4355
hlovdal wants to merge 6 commits intonode-red:devfrom
hlovdal:installing_esm_nodes

Conversation

@hlovdal
Copy link
Copy Markdown
Contributor

@hlovdal hlovdal commented Sep 19, 2023

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

Currently node-red fails when attempting to install a node written as an ESM module, e.g.

19 Sep 18:59:53 - [info] Server now running at http://127.0.0.1:1880/
19 Sep 18:59:53 - [info] Starting flows
19 Sep 18:59:53 - [info] Started flows
19 Sep 19:01:05 - [info] Installing module: @hlovdal/node-red-lowercase-in-typescript, version: 1.0.0
19 Sep 19:01:12 - [info] Installed module: @hlovdal/node-red-lowercase-in-typescript
19 Sep 19:01:12 - [info] Added node types:
19 Sep 19:01:12 - [info]  - @hlovdal/node-red-lowercase-in-typescript:lower-case : Error [ERR_REQUIRE_ESM]: require() of ES Module .../.node-red/node_modules/@hlovdal/node-red-lowercase-in-typescript/src/lower-case.js from .../node-red/packages/node_modules/@node-red/registry/lib/loader.js not supported.
Instead change the require of lower-case.js in .../node-red/packages/node_modules/@node-red/registry/lib/loader.js to a dynamic import() which is available in all CommonJS modules.

This pull request fixes that by using dynamic import() instead of require.

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

@hlovdal

This comment was marked as outdated.

@hlovdal

This comment was marked as outdated.

@hlovdal

This comment was marked as outdated.

@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 66af0c2 to 7f8730a Compare September 21, 2023 18:44
@hlovdal

This comment was marked as outdated.

Comment thread packages/node_modules/@node-red/registry/lib/loader.js Outdated
@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 468bdaf to 393658c Compare September 25, 2023 17:59
Copy link
Copy Markdown

@fbuys fbuys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am excited for this to be released, I left a comment if it helps.

Comment thread packages/node_modules/@node-red/registry/lib/loader.js Outdated
@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 393658c to 97a5fda Compare June 9, 2025 18:36
@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Jun 9, 2025

The original branch before the rebase is saved in my repo as installing_esm_nodes.2023.

@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 97a5fda to c4f6268 Compare July 4, 2025 19:55
Copy link
Copy Markdown

@Julian-Sz Julian-Sz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to this👍

@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Aug 21, 2025

Argh, the stupid update branch button has a default action to merge dev if you miss the drop down indicator on the side by a few pixels. Of course I intended to rebase. I'll fix it manually with a force push.

@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 2bf0fde to f8cbcb5 Compare August 21, 2025 20:42
@hlovdal hlovdal force-pushed the installing_esm_nodes branch 2 times, most recently from 368d6f0 to fb71032 Compare January 27, 2026 22:05
@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Jan 27, 2026

@knolleary

Can you please merge this pull request? It is 2026 and ESM is undeniably the future of javascript, and it has already been for a very, very, very long time. By not supporting nodes in ESM format node-red is preventing the world from progressing.

While ESM support slowly is adapted for require and this pull request perhaps is not strictly needed for V5, V4 supports node.js v18 which most certainly will not get support for that.

And even with the new esm support for require, merging this into V5 later will reduce version differences, and I would expect future undiscovered issues related to require(esm) to be much more probable than standard dynamic import which has been stable for years. But if you want this to stop in V4 and just rely on the behaviour of the runtime, that is a viable option. But for V4 this is an absolute must have functionality.

The branch is rebased to the latest V4 version and is now fully up to date. For testing you can use my lowercase ESM node. Or compile yourself from the wip/esm branch. The lowercase repo contains a full Cypress end to end test that installs the lowercase node and tests that it works. Update the run-node-red.sh to point to your node-red repo (with the installing_esm_nodes branch checked out and build).

@Julian-Sz
Copy link
Copy Markdown

I am also very much looking forward to this, hopefully it will get merged soon... 2.5 years and not much happened so far... :(

@knolleary knolleary added this to the 5.0 milestone Jan 28, 2026
@TotallyInformation
Copy link
Copy Markdown

There is an issue with the code I'm afraid. It will not work on Windows because imports require a URL, not a file path. The correct way is:

const { pathToFileURL } = require('node:url');

// ....

// let importPromise = import(node.file);
const modulePath = pathToFileURL(node.file).href;
let importPromise = import(modulePath);

@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Jan 28, 2026

@TotallyInformation Thank you for reporting. I will look into that (although possibly not immediately the first few days).

@hlovdal hlovdal force-pushed the installing_esm_nodes branch from 20c1e3e to 2bb7d82 Compare March 2, 2026 22:43
@hlovdal hlovdal force-pushed the installing_esm_nodes branch from a076a44 to a881659 Compare March 16, 2026 21:00
@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Mar 20, 2026

The windows issue has been fixed for a while. On my node-red node in typescript repo I have set up automatic tests for full end-2-end tests with Cypress, installing and testing an esm node on a node-red instance build from the installing_esm_nodes branch.

For now the windows runner times out because concurrently fails to terminate the node-red instance and I have not spent a lot of time trying to fix it. But the e2e test itself succeeds:

[CYPRESS]   ┌───────────────────────────────────────────────────────────────────────────────────────────
[CYPRESS]   │ ✔  upload_and_run.cy.ts                     01:08        1        1        -        -        - │
[CYPRESS]   └───────────────────────────────────────────────────────────────────────────────────────────
[CYPRESS]     ✔  All specs passed!                        01:08        1        1        -        -        -  
[CYPRESS] 
[CYPRESS] wait-on http://localhost:1880 && npm run cy:run exited with code 0
--> Sending SIGTERM to other processes..
Error: The operation was canceled.

@TotallyInformation Do you want to be the author of the commit that fixes the import? It is basically identical with your comment.

@TotallyInformation
Copy link
Copy Markdown

@TotallyInformation Do you want to be the author of the commit that fixes the import? It is basically identical with your comment.

No, that's fine. Thanks for the offer though.

Absolute file paths cannot be used on windows, and strictly speaking
using urls is the correct approach even if paths might work,
nodejs/node#34765 (comment).
@hlovdal hlovdal force-pushed the installing_esm_nodes branch from a881659 to c6c244b Compare April 18, 2026 13:57
@hlovdal
Copy link
Copy Markdown
Contributor Author

hlovdal commented Apr 18, 2026

All commits pass after the latest rebase.

git test add -t test "npm ci && npm run test"
git test run -t test dev..installing_esm_nodes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants