.JS file generates TypeError

I have been trying out the JSS functions. After reading several posts and taking up the hint to follow a npm tutorial, I am finally arriving at a state where is used npm install to install my own package. All errors referring to TypeError: Cannot load CommonJS module: 'openhab-tools' or Error: Invalid CommonJS root folder: /openhab/conf/automation/js/node_modules etc have gone which tells me that I am on the right track.

In my .js file concerning (future) timers I took a good peek at Rich Koshak’s openhab_rules_tools github page. My myTimers.js file now looks like this:

// Define array
let myTimers = [];

// Get a list of timers from the array
const listMyTimers = () => {
    console.debug("listMyTimer script started")

    for (var i = 0; i <myTimers.length; i++) {
        return myTimers[i][0], ", ", myTimers[i][1], ", ", myTimers[i][2], "\n";

module.exports = {

My index.js looks like this:

module.exports = {
    get myTimers() { return('./myTimers.js') }

In my rules I have:

var {listMyTimers} = require('@arjan-io/openhab-tools');

But is throws the following error:

Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: insertMyTimers is not a function

After visiting various (Stackoverflow) topics I still can’t figure out what I did wrong here, hence I turn to the community to see if any of you guys might be able to help…

Ps. the point is not if the code inside listMyItems is correct or useful but why this does not work in the first place

Ok, there were some errors I did correct.

  • myTimers.js => the return string is now correctly formatted
  • index.js => still the same
  • rule now reads var {myTimers} = require('@arjan-io/openhab-tools');

When I try to call var list = myTimers.listMyTimers(); I get the error. The problem lays somewhere in the file structure. This is confirmed by the fact that if I change var {myTimers} = require('@arjan-io/openhab-tools'); into var {myTimers} = require('@arjan-io/openhab-tools/myTimers.js'); it does work.

So the question becomes, why does the var {myTimers} = ... not pick up the myTimers.js file that sits in the same directory? a little help would be appreciated!

1 Like

Well, what you’ve posted so far is listMyTimers. There is no evidence of an exported function called insertMyTimers.

It’s not clear with what has been posted if you are having problems caused by missing functions in the exports or not.

Valid point. I’ve commented out large sections of my myTimers.js file in order to rule out any coding errors as much as possible.

Looks like I’ve given you guys a log section where I indeed tried a different const myFunc = () => {} than I posted as my example. Not very handy and my apologies. In the mean time I rechecked the lot and it keeps giving me the error if I use var {myTimers} = require('@arjan-io/openhab-tools');.

If I use var {myTimers} = require('@arjan-io/openhab-tools/myTimers.js'); (exact path to the myTimers’js file) I don’t get an error and the function works as advertised. Hence I arrived at the point where I would consider the file structure.

After using the install command, my package was installed at /openhab/conf/automation/js/node-modules/@arjan-io/openhab-tools/. Would that be the correct folder for a (scoped) package?

The error was in the code above. It was lacking require after return. Took me almost 2 days to figure that out. Pffff!