Homey Community Forum

External js files

I have different modules is my file app.js I would like to separate these in an external js file per module. Now my question is how do I link an external file to the main app.js file and vice versa.

In app.js you use require() to load external JS files (“modules”).

Those external JS files need to explicitly export functions/classes/variables to make them available in app.js:

// library.js
module.exports.helloWorld = function() {
  console.log('hello world');

// app.js
const { helloWorld } = require('./library.js');
// or:
// const helloWorld = require('./library.js').helloWorld;


Be aware that Node.js doesn’t support cyclic dependencies, meaning that if you load library.js into app.js you cannot also load app.js into library.js as well (because that introduces a cyclic dependency, where file A depends on file B which depends on file A which depends on file B etc)

More info here: Modules: CommonJS modules | Node.js v12.22.7 Documentation


but is it possible to read variables from app.js into a module?

Your module should export functions that take arguments, and you pass those variables as arguments to the functions.

Check out a example. GitHub - mickelluiten/com.satelintegra: Satel Integra Alarmsystem integration with Homey
In folder JS you find functions.js that is used is the app.js , driver.js and devices.js.

I’ve looked at it but it’s too complex. Isn’t it possible to give a simple example of a function in a module with two variables (from app.js) processed in a module and sent back to app.js

Perhaps it’s more beneficial for you to find a Node.js tutorial that covers external modules and functions. This isn’t the right place to learn JS and Node.js specifics.

1 Like