Packaging

Course- Node.JS >

Packaging


Node.js code is open and ready to be copied like any other Javascript code. But now its not possible anymore. JXcore which is an open source project, introduces a unique feature for packaging and encryption of source files and other assets into JX packages.

Consider you have a large project consisting of many files. JXcore is very much able to pack them all into a single file to simplify the distribution. This tutorial will give you a quick overview about the whole process starting from installing JXcore.

JXcore Installation

Installing a JXcore is as simple as just download and unpack the package which provide jx binary to start with. So let's follow the following steps:

Step 1

Download JXcore package from the http://jxcore.com/downloads/, as per your operating system and machine architecture. We downloaded a package for Cenots running on 64 bit machine.

$ wget https://s3.amazonaws.com/nodejx/jx_rh64.zip

Step 2

Unpack the downloaded file jx_rh64.zip and copy jx binary into /usr/bin or may be in any other directory based on your system setup.

$ unzip jx_rh64.zip
$ cp jx_rh64/jx /usr/bin

Step 3

Finally set your PATH variable appropriately to run jx from anywhere you like.

$ export PATH=$PATH:/usr/bin

Step 4

You can verify your installation by issuing a simple command. If everything is fine then you will find it working and printing its version number as follows:

$ jx --version
v0.10.32

Packaging Code

Consider you have following a project with the following directories where you kept your all the files including Node.js main file index.js and all the modules installed locally.

drwxr-xr-x  2 root root  4096 Nov 13 12:42 images
-rwxr-xr-x  1 root root 30457 Mar  6 12:19 index.htm
-rwxr-xr-x  1 root root 30452 Mar  1 12:54 index.js
drwxr-xr-x 23 root root  4096 Jan 15 03:48 node_modules
drwxr-xr-x  2 root root  4096 Mar 21 06:10 scripts
drwxr-xr-x  2 root root  4096 Feb 15 11:56 style

To package the above project, you simply need to go inside this directory and issue the following jx command, assuming index.js is the entry file for your Node.js project:

$ jx package index.js index

Here you could have used any other package name instead of index, I used it because I want keep my main file name as index.jx. However, above command will pack everything and will create following two files:

  • index.jxp This is an intermediate file whcih contains complete project detail needed to compile the project.

  • index.jx This is the binary file having complete package and ready to be shipped to your client or in your production environment.

Launching JX File

Consider your orignal Node.js project was running as follows:

$ node index.js command_line_arguments

Now after compiling your package using JXcore, it can be started as follows:

$ jx index.jx command_line_arguments

For a further detail on JXcore, you can check its official website.