# External js files

**URL:** <https://community.homey.app/t/external-js-files/54371>\
**Category:** Developers\
**Tags:** app\
**Created:** [November 6, 2021, 8:26am UTC](https://community.homey.app/t/external-js-files/54371 "2021-11-06T08:26:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agnes\_Descheerder](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/agnes_descheerder/32/46938_2.png) [@Agnes\_Descheerder](https://community.homey.app/u/Agnes_Descheerder)\
**Post date:** [November 6, 2021, 8:26am UTC](https://community.homey.app/t/external-js-files/54371/1 "2021-11-06T08:26:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 6, 2021, 8:29am UTC](https://community.homey.app/t/external-js-files/54371/2 "2021-11-06T08:29:21Z")

</div>

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`:

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

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

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](https://nodejs.org/docs/latest-v12.x/api/modules.html)

---

<div class="post-metadata">

**Author:** ![Agnes\_Descheerder](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/agnes_descheerder/32/46938_2.png) [@Agnes\_Descheerder](https://community.homey.app/u/Agnes_Descheerder)\
**Post date:** [November 6, 2021, 1:14pm UTC](https://community.homey.app/t/external-js-files/54371/3 "2021-11-06T13:14:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 6, 2021, 1:16pm UTC](https://community.homey.app/t/external-js-files/54371/4 "2021-11-06T13:16:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mickel\_Luiten](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mickel_luiten/32/12404_2.png) [@Mickel\_Luiten](https://community.homey.app/u/Mickel_Luiten)\
**Post date:** [November 6, 2021, 1:37pm UTC](https://community.homey.app/t/external-js-files/54371/5 "2021-11-06T13:37:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Agnes\_Descheerder](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/agnes_descheerder/32/46938_2.png) [@Agnes\_Descheerder](https://community.homey.app/u/Agnes_Descheerder)\
**Post date:** [November 6, 2021, 1:50pm UTC](https://community.homey.app/t/external-js-files/54371/6 "2021-11-06T13:50:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 6, 2021, 2:07pm UTC](https://community.homey.app/t/external-js-files/54371/7 "2021-11-06T14:07:09Z")

</div>

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.
