Hi Devs.
Hoping you can help: I'm only just learning this ESM/ CJS thing.
I'm trying to create a new node-red node based on a module https://www.npmjs.com/package/archer-c7-api,
The module is an 'ESM' but all example nodes I can find appear to be CJS (I think).
I've gone through a world of 'unfulfilled promise errors' or whatever.
The node-red call is async so I thought that I might be able to change the way I load the library but I can't work out what I'm doing wrong.
The example code from the library:
import ArcherC7 from './lib/ArcherC7.js';
(async function() {
const archer = new ArcherC7('http://192.168.1.1','email', 'password', { debug: true });
const devices = await archer.fetchDevices();
console.log(devices);
await archer.logout(); })();
What I'm trying to do in my node.js (haven't included the actual
module.exports = function(RED) {
var C7Devices = (async function(url,user,passwd) {
const A = await import( '../../node_modules/archer-c7-api/lib/ArcherC7.js' );
console.log ("ArcherC7 = ");
console.log ( A );
// ------?What to do here to instantiate an ArcherC7 object :confused:
const archer = await new A.ArcherC7('http://192.168.1.1', 'email', 'password', { debug: true });
const devices = await archer.fetchDevices();
console.log(devices);
await archer.logout();
return devices;
})().catch(err => console.error(err));
function ArcherC7Node(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
var pass = msg.payload.pass?msg.payload.pass:config.pass;
var user = msg.payload.user?msg.payload.user:config.user;
var url = msg.payload.url?msg.payload.url:config.url;
var devices = C7Devices(url,user,pass);
msg.payload = msg.payload.devices();
node.send(msg);
});
}
RED.nodes.registerType("ArcherC7Node",ArcherC7Node);
}
(removed most of the crazy attempt I've made at using this module).
Console output =
ArcherC7 =
[Module: null prototype] { default: [class ArcherC7] }
TypeError: A.ArcherC7 is not a constructor
at C:\Node\Demo\nodes\ArcherC7\ArcherC7.js:22:24
I can't work out how to create the class.
Can anyone suggest what I'm doing wrong? (am I wasting my time?)
Thanks in advance