Upgrade to Pro — share decks privately, control downloads, hide ads and more …

standalone executables for node.js apps

standalone executables for node.js apps

Sometimes you can’t just `npm install -g `. How can you accomplish that same instant gratification if nodejs and npm aren’t already installed? What if you could just attach a module to an email? Do you just give up and just switch to Go?

As it turns out, this has been [supported natively since 2011](https://github.com/joyent/node/commit/59f7232a302687754a3df577a0e3f1b04933445f) but lacks documentation and requires a bit of fiddling. This is a story about my recent adventures in node core, node-webkit, and black magic to [figure this out](https://github.com/imlucas/lone).

[slides from brooklynjs talk](http://imlucas.github.io/talks/brooklynjs_042014/static/)

Lucas Hrabovsky

August 14, 2014
Tweet

More Decks by Lucas Hrabovsky

Other Decks in Technology

Transcript

  1. want. run one command on any OS to get a

    single file that is ready to use with 0 prerequisites.
  2. single file? A.K.A:
 static binary
 standalone binary
 binary
 executable
 exe


    agent
 don’t have to do anything else to use it
 others?
  3. origin story • installing things is hard • especially in

    enterprise + pharma • want to just write a script and email a wget <url> command • no sudo required. no filenames to misspell
  4. origin story • figured out how to do this with

    python using esky • ported some chef recipes to python scripts • yay! we can just do this now • other things came up…
  5. later… • similar problem comes up at MongoDB • new

    thing to include in tarball everyone downloads • `go compile` can do this now • but young would have to fill in libraries and teach everyone golang
  6. few months later… • similar problem comes up at MongoDB

    • `go compile` can do this now • but young would have to fill in libraries and teach everyone golang. • wish I could just use node.js for this…
  7. other node.js reasons • Runs on any platform (win32, darwin,

    sunos, android, arm) • Reuse existing toolchains • Don’t have to wait for js hackers to grok a new language
  8. also: microsoft • vested interest in js for the web

    • already made substantial investment in node.js • Highly unlikely they would actually help Google make golang work well on win32 short-term, not to mention long-term.
  9. wait a tick! • if python and others can do

    this, surely someone has tried with node.js
  10. node-webkit • does this exact same thing • but I

    don’t need a UI • and 90MB of overhead would be a non-starter for a tar ball that’s already 125mb • maybe there’s something i can learn?
  11. how node-webkit does it • build nw.exe • zip application

    and deps • append zipped contents to nw.exe • when nw.exe runs main(), try to unzip itself
  12. trim down node-webkit? • try cherry picking what i need

    from node-webkit source • in just a few hours way more code than I want • picked up hints of what to google and grep for against node.js source
  13. _third_party_main.js • node.js was meant to do this all along!

    • (@ryah: I am so curious of the context for adding this…) • no official documentation though :( • just mailing list posts that it exists
  14. lone: prototype ! • copy and paste boot loader (_third_party_main.js)

    and a zip module (AdmZip) into ./lib • ./configure && make • create zip of app and dependencies • cat out/node app.zip > app
  15. it worked! • copy and pasting tedious • really wanted

    to write something, add to travis and forget about it • this whole thing was about making it as easy as possible to ship scripts • @brooklynjs talk time!
  16. ugh… • way too excited to give that talk •

    hadn’t fully baked the implications or a good way to explain it
  17. BUT • it was real! • my problems were solved!

    • continued to fiddle as needed to get a 1.0.0 release
  18. lone 1.0.0 • 1 open issue remaining • native add-on

    support • custom icons and metadata for exe’s on windows • making <myapp> —help not print node.js’s help • support any version of node.js
  19. lone all the things! #!/usr/bin/env bash ! git clone [email protected]:jshint/jshint.git

    ~/jshint cd ~/jshint && npm install npm install lone ./node_modules/.bin/lone ./.lone/.dist/jshint
  20. just a zip file! • so you can put anything

    in there • @todo: example mongodb.exe module that includes mongod.exe and mongos.exe from mongo tarball
  21. @todo • add examples that make a standalone binary from

    anything • prompts for a github url and where to clone to • runs lone • prompts for “run now?”
  22. next: mksnapshot • v8 precompile to byte code • can

    remove zip hack • faster startup time
  23. next: codesign • need to figure this out • people

    spooked by “install this exe”
  24. next: distribution • already have gulp tasks that uploads exe’s

    to github releases • really nice for development • npm run-script ci && npm run-script dist;
  25. next: oneline • support for the last mile • module

    you deploy to heroku or ec2 micro • proxy for github releases • “install button” or code snippet for README?
  26. even nexter? • REST api to compile so you don’t

    have to install python and visual studio on windows • node-pre-gyp? • how does this fit in with atom-shell & co?