Developing a project involving many flows, I needed to create function libraries, function arrays, and singleton objects (with data and methods), shared and accessible from each node/flow.
The 'standard' solution (import etc.) doesn't excite me.
I found a solution that works best for me: it uses singleton objects, and this is a good replacement for shared libraries, having the following advantages, also keeping the distribution issues in mind:
I tried it in a function and got the following crash, so luck so far!
7 Apr 17:28:26 - [error] TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
at new NodeError (node:internal/errors:372:5)
at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9)
at Function node:11896326bed805e9 [function 11]:2:42
at Function node:11896326bed805e9 [function 11]:22:3
at Script.runInContext (node:vm:139:12)
at processMessage (C:\Users\williams_b\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\nodes\core\function\10-function.js:401:33)
at FunctionNode._inputCallback (C:\Users\williams_b\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\nodes\core\function\10-function.js:335:17)
at Object.trigger (C:\Users\williams_b\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\util\lib\hooks.js:166:13)
at FunctionNode.Node._emitInput (C:\Users\williams_b\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:202:11)
Typescript (.ts) is precompiled source code.
what you see in Github is the "source code" - later (during publish/release) it is compile to JS files.
The main use for Typescript is the clue in its name Type. - it allows (during development) type safety, better error checking, its more for the developer than the end user - the End User will usually only deal with the resulting JS files.
I use Typescript during development, then compile to JS before releasing
That's 4 years out of date and has many unanswered issues sadly.
Basically, I cannot find any way to realistically migrate from CommonJS (const x = require('x')) to ESM in any meaningful way for something as complex as Node-RED. ES Modules can consume CommonJS ones easily enough but the reverse is not true.
I've no idea how Node-RED will be able to transition to ESM and I can't find a sensible way to create an ESM based node or even to incorporate an ESM only module such as the latest execa module.
While you can use a dynamic import on an ES Module, it requires you to effectively rewrite much of your node as async from the top down - just to accommodate a single ES Module.
And just make sure that you direct the typescript compiler to output CommonJS modules and then you won't have an issue.
Same here. But not for much longer because I think v14 LTS is near end of life. The next major version of uibuilder (some way off as I'm busy on smaller updates at present) will target v16 as one of its potentially breaking changes for example.
The next major release of Node-RED will also likely target v16. It too currently targets v14.