How to compile node.js files into binary executables (with pkg)

How to compile node.js files into binary executables


  1. Install pkg from npm with this command (don’t do it from the editor)
npm i pkg -D -S
  1. Run /app/node_modules/.bin/pkg -v to make sure it was installed correctly. It should return with a number (e.g 4.4.9).

  2. Compile the files! Navigate to the /tmp folder (cd /tmp) and run this command:

/app/node_modules/.bin/pkg /app/filename.js
Tips and tricks
  • The compiler will compilie for Windows, Mac, and Linix based systems. It cn take up to 172mb (even for the ~hello-express app!). So, try to specify one platform at a time by adding --node{version_number}-{platofrm}-{arch}
Platform Options

Platforms: freebsd, linux, alpine, macos, win
Archs: x64, x86, armv6, armv7
Node Versions: You pick!

  • To run your binary in Linux, type ./(Name Of Binary)
  • To run your binary in Windows, type the name of your binary.
1 Like

also putting this intresting thing out here, that does a bit more than package your app into an executable,

1 Like

Oh, yeah I heard of it. I just needed something really dead simple (so it does not kill Glitch). I’ll try to make a tutorial with Nectar later today/tommorow.

Quick Note: I suspect this may not account for integer overflow, I was doing a benchmark with sieve of erathoses and it took forever

1 Like