How to package your node.js application for deploying it in an offline environment?

Focus On Content  ~/ click me to toggle the navigation bar to the right

<img src="/img/2016/10/node2.png" alt="node2"> Personally speaking, I prefer to have a self-contained bundle with all the artifacts and modules that might be required to deploy an application (not just Node.js application) in Production. In that way, I know exactly the bits that were installed and nothing more and nothing less. It also eliminates the availability of the NPM modules and network connectivity issues, etc. The following procedure shows you how to create a simple “Hello World” Node.js application with one dependency - Express.js (which has dependency on other modules) and to bundle (pack) it and deploy it to an environment that may not have an internet connection.

Brief Summary: “bundledDependencies”: [“package-name1”] in the package.js does the trick in combination with “npm pack” and “npm install .tar.gz

Below is the step-by-step walk-thru.

So let’s get started: In Development Computer:

Make sure you have Node.js installed and you can verify the installation by running:


$node -v
v6.8.0

Now let’s get started on a simple “Hello World” - Node.js project:

Follow the steps outlined here for setting up a simple Hello World using Node.js and Express. In my case, running “npm install express –save” created additional module dependencies on the following:


$npm install express --save
guhelloproject
└─┬ [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
└── [email protected]

Now edit, package.js and add define the bundleDependencies section like so:


{
  "name": "guhelloproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"
  },
  "bundledDependencies": ["express"]
}

Let’s create index.js


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

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Test the code locally in development to ensure that it works.


$node index.js
Example app listening on port 3000!
# You should see "Hello World", if you point your browser to http://localhost:3000/

Now for the bundling magic.


$npm pack
guhelloproject-1.0.0.tgz

In your Production server (which has no internet connection):

  1. Make sure you install Node.js so that the Node executable is available. Verify once again by running “node -v” (as shown above)

  2. Move the “guhelloproject-1.0.0.tgz” file to the Production server.


$npm install guhelloproject-1.0.0.tgz
# This unzips the bundle into a 'node_modules' directory 
$cd cd node_modules/guhelloproject/
$node index.js
Example app listening on port 3000!

Point your browser to http://:3000/ and you should see “hello world”.