ioBroker / ioBroker.javascript

Script engine for JavaScript and Blockly
MIT License
325 stars 120 forks source link

[Bug]: moment-duration-format generates script error with js-controller 6.x #1613

Open MK-66 opened 3 months ago

MK-66 commented 3 months ago

I'm sure that

Script type

JavaScript

The problem

With js-controller 6.x, a script with the npm module moment-duration-format generates following error: error: moment.duration(...).format is not a function.

The npm modules moment and moment-duration-format are installed in the javascript adapter and no errors are output when the adapter is started, only the script itself generates the error in the log. Moment and moment.duration do not generate any errors in the script, the error only occurs with moment.duration.format.

Sript line: let uptime = (existsState(id + '.uptime')) ? moment.duration(getState(id + '.uptime').val, 'seconds').format("D[T] H[h] m[m]", 0) : '-';

The same script works normally under js-controller 5.0.19 without any errors.

Screenshot npm-module javascript adapter:

Screenshot javascript moduls

iobroker.current.log (in debug mode!)

Full error log entry of the script: script.js.MD.MD_AdapterStatus_TEST: [adapterStatus] error:moment.duration(...).format is not a function, stack: TypeError: moment.duration(...).format is not a function at adapterStatus (script.js.MD.MD_AdapterStatus_TEST:248:115) at script.js.MD.MD_AdapterStatus_TEST:561:1 at script.js.MD.MD_AdapterStatus_TEST:711:3 at Script.runInContext (node:vm:148:12) at Script.runInNewContext (node:vm:153:17) at execute (/opt/iobroker/node_modules/iobroker.javascript/main.js:1968:23) at prepareScript (/opt/iobroker/node_modules/iobroker.javascript/main.js:2215:13) at /opt/iobroker/node_modules/iobroker.javascript/main.js:2307:17 at Immediate._onImmediate (/opt/iobroker/node_modules/iobroker.javascript/main.js:1722:17) at processImmediate (node:internal/timers:480:21)

Version of nodejs

20.14.0

Version of ioBroker js-controller

6.0.4

Version of adapter

8.6.0

klein0r commented 3 months ago

Looks like moment has no default export

Suggestions @foxriver76 ?

foxriver76 commented 3 months ago

https://www.npmjs.com/package/moment-duration-format#module This works correct, controller v6.0.5, js adapter 8.6.0

grafik

So maybe normally it had some side effects automatically which now need to be configured explicitly but for this we would need to see your code @MK-66

foxriver76 commented 3 months ago

This also works

console.log(moment.duration(123, "minutes").format("h:mm"));

So maybe you just need momentDurationFormatSetup(moment); after the imports.

MK-66 commented 3 months ago

@foxriver76, @klein0r The line momentDurationFormatSetup(moment); is obviously exactly the right one that was missing in the script. Unfortunately I'm not a javascript pro, so thanks for the hint. Issue can then be closed from my point of view, should I close the issue then?

foxriver76 commented 3 months ago

If this happens to a lot of users we will think about adding a short news on how to fix it, please leave it open until we close it.

klein0r commented 3 months ago

please leave it open

sorry 😄

agross commented 2 months ago

Probably related: #1642