# \[Tool\] Package Bundler — potentially shrink your app size drastically with this tool

**URL:** <https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460>\
**Category:** Developers\
**Tags:** tools, homey-pro, homey-bridge\
**Created:** [November 21, 2022, 2:40pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460 "2022-11-21T14:40:58Z")\
**Posts on this page:** 20\
**Page:** 1

<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 21, 2022, 2:40pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/1 "2022-11-21T14:40:58Z")

</div>

## Package Bundler

Github: [GitHub - robertklep/package-bundler: Bundle a Node.js package into a single file.](https://github.com/robertklep/package-bundler)

This tool can be used to create single-file Node.js packages from existing (NPM-hosted) packages.

I’ll explain why this can be useful with an example:

At the moment I’m working on an app that depends on [`homey-api`](https://www.npmjs.com/package/homey-api). Just adding this package to my project made the installation time of my app (`homey app run`) explode into multiple dozens of seconds.

The reason is two-fold: `homey-api` and all its dependencies take up 7MB of disk space, and consists of about 3500 files (this is the main performance killer when it comes to Homey apps).

By bundling `homey-api` into a single file, its size is cut down to about 600K, and just a single file. See below for a “benchmark”.

### How to use

Follow the installation instructions on the Github page.

To bundle `homey-api`, run the following command:

```auto
$ package-bundler homey-api

```

This will create a directory `homey-api` in the current directory that contains the bundled file (`index.js`). If you move this directory into your app directory, you can use it from your app code as a regular relative import:

```auto
// app.js
const { HomeyAPIApp } = require('./homey-api');

```

Don’t forget to remove `homey-api` as a dependency of your app, otherwise it will still be uploaded:

```auto
$ npm uninstall homey-api

```

(if you for some reason want to remove the package(s) manually, don’t just remove them from `node_modules` without updating `package.json`, because you’ll get an error about missing requirements when you run `homey app run`).

### Some remarks

- It should be no problem to publish apps to the App Store with bundled packages like this.
- Not all packages can be bundled, especially packages that use dynamic imports.
- Not all packages _have_ to be bundled. Just pick the ones that are large and/or consist of a lot of files.
- A bundled package always requires a relative import, so if you want to use it from a driver/device file, you need to use something like `require('../../homey-api')` (or possibly `require('/homey-api')`).
- Instead of placing bundled packages directly in your app directory, I would suggest placing them in a subdirectory (say `modules/`) to keep things nicely separated. Adjust your import paths accordingly.
- I haven’t tested this script very well and wrote it mainly for my own purpose. If you run into any issues, hit me up.

### “Benchmark”

Let’s test how long it takes for `homey app run` to install/start/stop an app that does absolutely nothing other than loading `homey-api`.

- Unbundled:

```auto
$ time homey app run
✓ Pre-processing app...
 — App archive size: 6.9 MB, 3521 files
✓ Running `my.test.app`, press CTRL+C to quit
✓ Uninstalling `my.test.app`...
✓ Homey App `my.test.app` successfully uninstalled
homey app run 1.71s user 1.11s system 6% cpu 40.349 total

```

- Bundled:

```auto
$ time homey app run
✓ Pre-processing app...
 — App archive size: 703 KB, 10 files
✓ Running `my.test.app`, press CTRL+C to quit
✓ Uninstalling `my.test.app`...
✓ Homey App `my.test.app` successfully uninstalled
homey app run 0.77s user 0.16s system 13% cpu 6.631 total

```

So the unbundled version takes 40 seconds, the bundled version takes 6 seconds. And that’s including uninstallation too, which is relatively slow. The time until the app is uploaded, installed and started takes about 35 seconds unbundled, and 2 seconds bundled.

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [November 21, 2022, 11:09pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/2 "2022-11-21T23:09:12Z")

</div>

I would love to use this, but since I’m on Windows I don’t know how to use this. Looks like this will only work on Linux?

Thanks though, I can imagine this could improve many apps!

---

<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 22, 2022, 6:13am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/3 "2022-11-22T06:13:59Z")

</div>

I think it might work when you install [WSL](https://learn.microsoft.com/en-us/windows/wsl/install). I run this script on macOS myself.

Or, if you don’t get it working, let me know which module(s) you would like to try with it and I can bundle them for you.

---

<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 23, 2022, 7:28am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/4 "2022-11-23T07:28:11Z")

</div>

I’ve rewritten the script from a Bash script to a Node.js script, which should hopefully make it easier to install and run on Windows. See the updated installation instructions [on Github](https://github.com/robertklep/package-bundler#installation).

---

<div class="post-metadata">

**Author:** ![Jeroen\_Tebbens](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jeroen_tebbens/32/62246_2.png) [@Jeroen\_Tebbens](https://community.homey.app/u/Jeroen_Tebbens)\
**Post date:** [November 23, 2022, 8:29am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/5 "2022-11-23T08:29:37Z")

</div>

@robertklep great find! Do you think node-fetch would benefit as well?

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [November 23, 2022, 9:22am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/6 "2022-11-23T09:22:01Z")

</div>

> [@Le\_Cactus](#):
>
> I would love to use this, but since I’m on Windows I don’t know how to use this. Looks like this will only work on Linux?

As @robertklep says, WSL is the way to go on Windows. It took me about 2 hours of hacking this morning but I got there eventually. I would put some steps to make it work but I’m not entirely sure how I got there 🙂

Edit: I have just seen @robertklep update where he has converted it to a node script so should be really simple now and not require WSL If only I had waited an extra hour before starting 😆

---

<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 23, 2022, 10:25am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/7 "2022-11-23T10:25:07Z")

</div>

Sorry 😂

I’m also planning to remove the external dependency on `@vercel/ncc`, but that’s not a major change.

---

<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 23, 2022, 10:32am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/8 "2022-11-23T10:32:38Z")

</div>

Yes, I just tried, but since only CommonJS packages are supported (as of yet) you need to install `node-fetch@2` (as per the [installation instructions](https://github.com/node-fetch/node-fetch#commonjs)):

```auto
$ package-bundler node-fetch@2

```

The bundled package that comes out is 300K and a quick test shows it works just fine.

---

<div class="post-metadata">

**Author:** ![Jeroen\_Tebbens](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jeroen_tebbens/32/62246_2.png) [@Jeroen\_Tebbens](https://community.homey.app/u/Jeroen_Tebbens)\
**Post date:** [November 23, 2022, 10:40am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/9 "2022-11-23T10:40:50Z")

</div>

Thanks for confirmation. Tempted to try it on my Homewizard app as its over 1000 lines and 4Mbyte total. So curious if it will benefit from it. Did you also see a drop in memory usage for your app or does it only affect the used storage for the application?

---

<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 23, 2022, 10:42am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/10 "2022-11-23T10:42:54Z")

</div>

I haven’t really checked if it uses less memory, but it probably won’t (or at least not a considerable amount). But it makes the developer experience so much better, no more 40-second waits to upload your app to Homey after a small change.

---

<div class="post-metadata">

**Author:** ![Jeroen\_Tebbens](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jeroen_tebbens/32/62246_2.png) [@Jeroen\_Tebbens](https://community.homey.app/u/Jeroen_Tebbens)\
**Post date:** [November 23, 2022, 10:45am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/11 "2022-11-23T10:45:56Z")

</div>

Agreed, during development and testing of my app stop/start is eh…taking time indeed. 🙂

---

<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 23, 2022, 12:11pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/12 "2022-11-23T12:11:03Z")

</div>

> [@robertklep](#):
>
> I’m also planning to remove the external dependency on `@vercel/ncc`

Done.

---

<div class="post-metadata">

**Author:** ![DaneedeKruyff](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/daneedekruyff/32/964_2.png) [@DaneedeKruyff](https://community.homey.app/u/DaneedeKruyff)\
**Post date:** [November 23, 2022, 6:28pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/13 "2022-11-23T18:28:56Z")

</div>

Haven’t been able to try this yet, but I think if I wait a bit more all will be done automatically 😜

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [November 23, 2022, 6:40pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/14 "2022-11-23T18:40:01Z")

</div>

I can’t get this to work, undoubtably I am doing something wrong…

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/5/055d22a4a1ba3fa0b864613420af14d50a877967.png)

So far so good. I then removed the existing luxon folder from the node\_module folder, and changes the “const luxon = require(‘luxon’);” to “const luxon = require(‘./luxon’);” in the app.js

But app validation/installation now fails:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/6/d64d5e5a2592bf114bccce47bf146c56b9335d75.png)

Any hints are much appreciated.

---

<div class="post-metadata">

**Author:** ![Robin\_van\_Kekem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robin_van_kekem/32/8187_2.png) [@Robin\_van\_Kekem](https://community.homey.app/u/Robin_van_Kekem)\
**Post date:** [November 23, 2022, 7:48pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/15 "2022-11-23T19:48:30Z")

</div>

I’m no node.js expert but aren’t your dependencies defined in package.json?

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [November 23, 2022, 7:57pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/16 "2022-11-23T19:57:22Z")

</div>

Not with Homey apps afaik. The example in the opening post also mentions the app.js.

---

<div class="post-metadata">

**Author:** ![Robin\_van\_Kekem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robin_van_kekem/32/8187_2.png) [@Robin\_van\_Kekem](https://community.homey.app/u/Robin_van_Kekem)\
**Post date:** [November 23, 2022, 8:00pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/17 "2022-11-23T20:00:00Z")

</div>

Did you try?

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [November 23, 2022, 10:16pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/18 "2022-11-23T22:16:50Z")

</div>

As @Robin_van_Kekem said, make sure you remove the dependency line in package.json  
If you used npm to uninstall tha package it would have done that for you, but if you just deleted the folder then it will still be there.

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [November 23, 2022, 11:19pm UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/19 "2022-11-23T23:19:57Z")

</div>

Yup, that solved it. Thanks!

---

<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 24, 2022, 6:21am UTC](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460/20 "2022-11-24T06:21:23Z")

</div>

It’s [in the fine manual](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460#how-to-use-2) 😉

[Next page](https://community.homey.app/t/tool-package-bundler-potentially-shrink-your-app-size-drastically-with-this-tool/72460.md?page=2)
