How to compile node.js files into binary executables
- Install
pkg
from npm with this command (don’t do it from the editor)
npm i pkg -D -S
-
Run
/app/node_modules/.bin/pkg -v
to make sure it was installed correctly. It should return with a number (e.g4.4.9
). -
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.