Reusing functions in JavaScript

It sounds like a FAQ, but haven’t been able to find a solution, so now asking here: What is the best approach for reusing functions “ad-hoc” with JavaScript Scripting?

On my first day of playing with JavaScript last Christmas, I initially created this in a file in directory node_modules:

exports.send = function(title, body) {
        var action = actions.thingActions('pushover', 'pushover:pushover-account:account');
        action.sendMessage(body, title);
}

which I could then reuse from my scripts by:

var notification = require('notification.js');

I have now learned that this was a terrible idea because node modules are managed and should be installed in proper way.

In the documentation I can see that one could create a library, package it and install it. However, this seems like a lot of hassle in order to simply be able to reuse my own functions which are already managed in a git repository.

Is there a more “light weight”/ad-hoc way of reusing functions between files?

Well, you can never use npm to ever install node modules (note that openHABian installs npm libraries in that folder for you).

The problem is how npm and Node.js works (and the realization that JS Scripting provides a Node.js like environment). If you install a module using npm, it’s going to uninstall any module that wasn’t installed using npm, deleting your library.

This was a deliberate choice made by Node.js, not us.

Note, the instructions you link to only has to be done once. After you’ve installed your personal library, you can edit the files in the folder under automation/js/node_modules as much as you want and they will be safe.

It’s not a matter of management. It’s setting up and configuring the files and directory structure required so that npm recognizes your library as legitimate and doesn’t delete it.

You could do it all by hand, creating the package.json file in your library’s folder by hand and hand editing the package.json and package-lock.json files in the automation/js folder. But it’s way less error prone and way less work to do it following the instructions.

And again, you only have to do this once. After it’s “installed” you can create/delete/edit the files in place.

1 Like

Thanks for this hint, I should have RTFM. I somehow missed bullet 6 in that chapter, so that basically renders this a non-issue. Doing it once is not a problem at all, I was simply not ready to make that investment if I had to go through the hassle each time I wanted to add a new function or a new file.

EDIT: I probably missed it because I immediately gave up when reading bullet 5. :grinning:

Now having done this, one small follow-up question. I now have my legitimate work directory within node_modules/jlaur - this part is fine. But automation/js turned a bit into a mess (excluding my other scripts):

drwxrwxr-x 3 openhabian openhab 4.0K Dec 28 21:09 node_modules/
-rw-r–r-- 1 openhabian openhab 76 Dec 28 21:09 package.json
-rw-r–r-- 1 openhabian openhab 656 Dec 28 21:09 package-lock.json

Inside both of these package files are relative references to the file jlaur-1.0.0.tgz which I created in a temporary directory, planning to remove it again.

Can I safely remove this file again? If I can, should I still leave package.json and package-lock.json as is, declaring a dependency towards a non-existing file? Or is it enough keeping node_modules/.package-lock.json (although also having the same file reference)?

That’s a good question. I never removed mine out of lazyness. I’m not sure what will happen if you remove it. Try it and see (take a backup first of course).

It seems reasonable that it will leave your folder alone since there’s nothing available to replace the library so I bet it’ll be fine.

I guess you can remove the temporary folder together with the .tgz, but you shouldn’t delete the package files.

1 Like