• Node.js Video Tutorials

Node.js - Packaging



A large-sized Node.js project usually has a number of dependencies and a number of source files as well as other assets such as images, web pages, setting files etc. To distribute a Node.js project and deploy it on any other environment becomes difficult, and hence it needs to packaged so that it can be easily ported to other machine. There are a number of packaging tools available on NPM repository. This chapter discusses nexe packaging tool, and also takes overview of some other packaging libraries.

Nexe

To demonstrate how the Nexe utility works, we shall an ExpressJs app with the following script −

Index.js

var express = require('express');
var app = express();
var path = require('path');

var bodyParser = require('body-parser');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

app.use(express.static('public'));

app.get('/', function (req, res) {
   res.sendFile(path.join(__dirname,"index.html"));
})

app.get('/process_get', function (req, res) {
   // Prepare output in JSON format
   response = {
      first_name:req.query.first_name,
      last_name:req.query.last_name
   };
   console.log(response);
   res.end(JSON.stringify(response));
})

app.post("/process_post", )
var server = app.listen(5000, function () {
   console.log("Express App running at http://127.0.0.1:5000/");
})

The ‘/’ route renders a HTML form from the following script −

Index.html

<html>
   <body>
      
      <form action = "/process_POST" method = "POST">
         First Name: <input type = "text" name = "first_name">  <br>
         Last Name: <input type = "text" name = "last_name">  <br>
         <input type = "submit" value = "Submit">
      </form>
      
   </body>
</html>

The Node.js server displays the following form −

Nexe Utility

The Node.js application above also registers the static middleware, hence it displays an image placed in static/images folder.

Tutorialspoint Logo

We now want to build a self-executable for this Node.js app containing the code, the subdirectories as well as the assets in a single file. Nexe is a command-line utility that compiles your Node.js application into a single executable file.

Features of Nexe

  • Self contained applications

  • Ability to run multiple applications with different node.js runtimes.

  • Distribute binaries without needing node / npm.

  • Start and deploy faster.

  • Flexible build pipeline

  • Cross platform builds

Install Nexe on Windows

Perform global installation of Nexe as follows −

npm install nexe -g

You also need Netwide Assembler (NASM) tool. Download and install the same from www.nasm.us

The nexe utility requires Python installed on the system. Ensure that Python is one of the versions 3.11 to 3.7.

Assuming that you are using nexe for 64-bit Windows OS, you also need to install The "Desktop development with C++" workload from Visual Studio 2022. It can be installed from aka.ms.

(For other OS platforms, follow the instructions on github.com/nodejs

Once all the prerequisites are installed, run the following command in the CMD terminal −

D:\expressApp>nexe index.js --build windows-x64-20.9.0 –verbose

The compilation may take a while, but in the end, it will create expressApp.exe in the application folder.

D:\expressApp
│   expressApp.exe
│   index.html
│   index.js
│   package-lock.json
│   package.json
│   users.json
│
├───node_modules
│   │   .package-lock.json
│   ├───body-parser
│   │   │   HISTORY.md
│   │   │   index.js
│   │   │   LICENSE
│   │   │   package.json
│   │   │   README.md
│   │   │   SECURITY.md
│   │   │   . . .
│   │   │   . . .
│   │   │   . . .
│   ├───express
│   │   │   History.md
│   │   │   index.js
│   │   │   LICENSE
│   │   │   package.json
│   │   │   Readme.md
│   │   │   . . .
│   │   │   . . .
│   │   │   . . .
└───public
    └───images
            logo.png

Run it from the command line, and the Node.js server starts.

D:\expressApp>expressapp
Express App running at http://127.0.0.1:5000/

pkg tool

The pkg tool is a command-line interface that enables the developer to create executables from Node.JS projects; allowing you to run the app even on environments that do not have Node.JS installed on them.

To install pkg, use the command −

npm install -g pkg

Then use pkg to build the executable

pkg myapp.js

Running the above command will generate three programmes; namely an executable for Windows, macOS, and Linux. More details can be found at www.npmjs.com.

JXCore

JXcore, which is an open source project, introduces a unique feature for packaging and encryption of source files and other assets into JX packages.

Download and install the JXcore package from https://github.com/jxcore, as per your operating system and machine architecture.

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

the above command will pack everything and will create the following two files −

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

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

Consider your original Node.js project was running as follows −

node index.js command_line_arguments

After compiling your package using JXcore, it can be started as follows −

jx index.jx command_line_arguments
Advertisements