DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Mastering Node.js: The Ultimate Guide
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • Using Render Log Streams to Log to Papertrail
  • How To Use the Node Docker Official Image

Trending

  • Build Quicker With Zipper: Building a Ping Pong Ranking App Using TypeScript Functions
  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing
  • How To Validate Archives and Identify Invalid Documents in Java
  • AWS Amplify: A Comprehensive Guide
  1. DZone
  2. Coding
  3. JavaScript
  4. Compiling a nodejs projects as a single binary

Compiling a nodejs projects as a single binary

Patrick Debois user avatar by
Patrick Debois
·
May. 16, 13 · Tutorial
Like (0)
Save
Tweet
Share
30.67K Views

Join the DZone community and get the full member experience.

Join For Free

Let's face it, if you write software it's often hard to distribute it: you have the runtime , the modules you depend on and your software itself. Sure you can package that all but packages ofter require you to have root-privileges to install.

Therefore at times it's convenient to have a single file/binary distribution. Download the executable and run it. For ruby project you can convert things into a single jar using Jruby. A good example is the logstash project: download 1 file , run it and you're in business. But you'd still require the java runtime to be installed. (thanks Apple, NOT).

This is a extra of the GO language but I was looking for a similar thing for nodejs. And the following documentation is the closest I could it get: (it works!)

Compiling plain javascript (no external modules)

Enter nexe a tool to compile nodejs projects to an executable binary.

The way it works is: - it downloads the nodejs source of your choice - 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 directory)

Creating a binary is as simple as:

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


Caveats:

  • I had an issue with unicode chars that got converted: it uses uglify.js and this needs to be configured to leave them alone Sardines Patch Unichode . This was necessary to get terminal.js to compile
  • Next issue was to get socket.io-client to compile: the swfobject has document and navigator objects, so this had to be fixed as well - Sardines Patch Document & Navigator

Alternatives:

  • Node-webkit to package nodejs apps that require UI interaction
  • http://tidesdk.multipart.net/docs/user-dev/generated/ - seems similar but could not really grasp it
  • AppJS - http://appjs.org/#why - aims to create HTML5/Javascript native apps
  • NPKG - https://github.com/wearefractal/npkg - old but interesting code

Embedding a native module (in the nodejs binary)

Many of these single packaging tools, suffer from the problem of handline native modules.

nexe doesn't handle native modules (yet).

But with a little persistance and creativity, 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 in the src directory
# If there is a header file copy/adapt it too
$ 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

# Add the pty.js to the node.gyp
# Somewhere in the library list add pty.js
# Somewhere in the source list add node_pty.cc

# 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.

$ make clean
$ ./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)

Now you need to remove the native dependency from your package.json before you nexe build it

Packaging the file

A single binary now makes it easy to to make a curl installer from it as it only requires you to download file. Remember the caveat of this.

And you can still package it up:

  • create a rpm, deb, etc.. package from it using fpm
  • or create a native MacOSX .app file from it as Matthias Bynens suggest in http://mathiasbynens.be/notes/shell-script-mac-apps
  • https://github.com/subtleGradient/Appify-UI
  • http://blog.coolaj86.com/articles/how-to-create-an-osx-pkg-installer.html
  • build a DMG - http://www.recital.com/index.php?option=com_content&view=article&id=108%3Ahowto-build-a-dmg-file-from-the-command-line-on-mac-os-x&Itemid=59

Extras

Rant about why it's a good or bad Idea - Secure Nodejs distribution

More info on the process.binding:

  • http://blog.carbonfive.com/2011/03/14/node-js-part-ii-spelunking-in-the-code/
  • https://groups.google.com/forum/?fromgroups#!topic/nodejs/R5fDzBr0eEk

Convert nodejs projects to single file/beautifier:

  • Npk - https://github.com/cfsghost/npk
  • UglifyJS - https://github.com/mishoo/UglifyJS/
  • RequireJS - http://requirejs.org/
  • Browserify - http://browserify.org/
  • OneJS - https://github.com/azer/onejs

Cross compiling:

  • https://github.com/felixge/node-cross-compiler
  • http://n8.io/cross-compiling-nodejs-v0.8/
Node.js

Published at DZone with permission of Patrick Debois, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Node.js: The Ultimate Guide
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • Using Render Log Streams to Log to Papertrail
  • How To Use the Node Docker Official Image

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: