Home » Nodejs » how to compile nodejs to a single, fully static binary file

how to compile nodejs to a single, fully static binary file

Posted by: admin November 30, 2017 Leave a comment

Questions:

Does anyone know how to statically compile nodejs to a single executable binary?
I means no share libraries needed.

There are some guides for old version nodejs, but not work for last one.

Thanks!

Answers:

Actually, I want a single static binary to run on android platform.

And now , up to version 0.11.8, node.js officially add android support.

my colleague build it not only arm, but also android-x86. everything works fine!

https://github.com/joyent/node/commit/5e4e8ec429381a8d1eebe31647e9daaaae42c54b

Questions:
Answers:

You can use nodejs-static docker image to get statically compiled Node.JS binary linked with musl libc.

https://hub.docker.com/r/dundee/nodejs-static/

Questions:
Answers:

For those who still find this topic relevant there is a project called node-packer, that compiles javascript code along with the nodejs binary available here https://github.com/pmq20/node-packer but in order to have a truly static build one needs to modify the ruby script and add the flags –fully-static and –without-npm which should enable the binary to run without libstdc++ or glibc, but there are some caveats as explained on the nodejs wiki. I have also already made the modification here (https://github.com/cmosh/node-packer), just append the flag –static to the command, i.e nodec –static foo.js. Also in order to run this you will need to install both ruby and nodejs and run nodec script within the bin folder of the cloned repo.

Questions:
Answers:

Meanwhile there is http://enclosejs.com/ which seems to be able to generate a single binary.

Questions:
Answers:

Here’s another one that can package your application in a single executable https://www.npmjs.com/package/pkg