If you’re writing command line tools in Node.js, it can be hard to distribute them since users need to install Node.js in their machines before being able to use your tool. It would be a lot easier for users if we can package our app into a single executable file that they can download and run without installing anything extra.
We can use pkg to compile our code into a single executable file for multiple target platforms (Windows, Linux, Mac etc).
Let’s start with a simple example:
This file prints “hello world” and exits. We can package this by running:
npx pkg index.js
This by default builds executables for three platforms - Windows, Linux and Mac:
The target platforms can be customized by using the
I was curious how much space these take up:
22-34MB feels like a bit too much for something that just prints “hello world”. Looking around the internet, it seems we can use tools like upx to reduce the file size.