pkg allows you to package your node project into a compiled app. Compiling your app has some great benefits including:
- Make a commercial or demo versions of your application without sources
- Instantly make executables for other platforms (cross-compilation)
- No need to install Node.js and npm to run the packaged application
There are lots of use cases for this, one of ours is a tiny app that we can run to create and restore snapshots of droplets. If we deploy some breaking changes to an instance, we can roll back those changes quickly with a snapshot. This is a lifesaver when dealing with small quirky applications and allows us to minimize downtime on projects that don’t have sophisticated release cycles.
For example, we can run this app on any of our machines:
This will create a snapshot of the droplet 1234.
We can restore that snapshot like so:
You can run this by hand, from a scheduled AWS Lambda function or a good ol’ cron.
The build process is really simple. Let’s say you had this as `app.js`
Run this command:
Within your current directory, you will have three binaries:
So, you can run
All you need to do is drop the compiled app on the target machine and you are ready to go. No dependencies and time saved without having to run the npm install.