ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

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

Open MK-66 opened this issue 1 year ago • 7 comments

I'm sure that

  • [X] This issue is still present in the current beta version of this adapter
  • [X] There is no other (open) issue with the same topic (use the search!)
  • [X] This issue is not described in the adapter documentation / FAQ (read the docs!)

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

MK-66 avatar Jun 15 '24 13:06 MK-66

Looks like moment has no default export

Suggestions @foxriver76 ?

klein0r avatar Jun 17 '24 10:06 klein0r

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 avatar Jun 17 '24 10:06 foxriver76

This also works

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

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

foxriver76 avatar Jun 17 '24 10:06 foxriver76

@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?

MK-66 avatar Jun 17 '24 15:06 MK-66

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.

foxriver76 avatar Jun 18 '24 06:06 foxriver76

please leave it open

sorry 😄

klein0r avatar Jun 18 '24 06:06 klein0r

Probably related: #1642

agross avatar Jul 19 '24 19:07 agross