Skip to content

Instantly share code, notes, and snippets.

@jedi4ever
Last active October 10, 2020 15:12
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedi4ever/5569381 to your computer and use it in GitHub Desktop.
Save jedi4ever/5569381 to your computer and use it in GitHub Desktop.
Notes on compiling/packaging a node.js projects as a single binary

Compiling plain javascript (no external modules)

So we got nexe to compile nodejs projects to an executable binary:

  • it downloads the nodejs source
  • it creates a single file nodejs source (using sardines )
  • it monkey patches the nodejs code to include this single file in the binary (adding it to the lib/nexe.js)

$ nexe -i myproject.js -o myproject.bin -r 0.10.3

Caveats:

Alternatives:

Embedding a native module (in the nodejs binary)

  • nexe doesn't handle native modules (yet).

This is what I did to add the pty.js native module directly to the nodejs binary

$ tar -xzvf node-v0.8.21.tar.gz
$ cd node-v0.8.21

# Copy the native code 
$ cp ~/dev/terminal.js/node_modules/pty.js/src/unix/pty.cc src/node_pty.cc
# Correct the export name of the module
# Add the node_ prefix to the node_module name
# Last line should read - NODE_MODULE(node_pty, init)

# add node_pty to src/node_extensions.h (f.e. right after node_zlib)
# NODE_EXT_LIST_ITEM(node_pty)

# Copy the pty.js file
$ cp ~/dev/pty.js/lib/pty.js lib/pty.js

# Adapt the namings/bindings in lib/pty.js
# 1) replace: var pty = require('../build/Release/pty.node');
#    with: var binding = process.binding('pty');
# 2) replace all references to pty. to binding.

$ ./configure
$ make

Now you have a custom build node in out/Release/node The filesize was about 10034856 , you can further strip it and 6971192 (6.6M)

Info on the process.binding:

Packaging the file

Extras

@typon
Copy link

typon commented Mar 26, 2017

Once you have compiled node with the native modules embedded, how do you point nexe to use it for compiling with your actual application?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment