Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Package Your Node.js Application for Deployment in an Offline Environment

DZone's Guide to

Package Your Node.js Application for Deployment in an Offline Environment

See how to create a simple ''Hello World'' Node.js application and deploy it to an environment that may not have an internet connection.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Personally speaking, I prefer to have a self-contained bundle with all of the artifacts and modules that might be required in order to deploy an application (not just the Node.js application) in production. In that way, I am able to know exactly the bits that were installed. I will know nothing more and nothing less. It also eliminates the availability of the NPM modules, network connectivity issues, and so on. The following procedure shows you how to create a simple “Hello World” Node.js application with one dependency, Express.js (which has a dependency on other modules), and how to bundle (pack) it and deploy it to an environment that may or may not have an internet connection.

Brief Summary

The “bundledDependencies”: [“package-name1”] in the package.js does the trick in combination with “npm pack” and “npm install <Project>.tar.gz.

Below is the step-by-step walk-through, so let’s get started.

In Your Development Computer

Make sure you have Node.js installed and that 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 that have been 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
└─┬ express@4.14.0
├─┬ accepts@1.3.3
│ ├─┬ mime-types@2.1.12
│ │ └── mime-db@1.24.0
│ └── negotiator@0.6.1
├── array-flatten@1.1.1
├── content-disposition@0.5.1
├── content-type@1.0.2
├── cookie@0.3.1
├── cookie-signature@1.0.6
├─┬ debug@2.2.0
│ └── ms@0.7.1
├── depd@1.1.0
├── encodeurl@1.0.1
├── escape-html@1.0.3
├── etag@1.7.0
├─┬ finalhandler@0.5.0
│ ├── statuses@1.3.0
│ └── unpipe@1.0.0
├── fresh@0.3.0
├── merge-descriptors@1.0.1
├── methods@1.1.2
├─┬ on-finished@2.3.0
│ └── ee-first@1.1.1
├── parseurl@1.3.1
├── path-to-regexp@0.1.7
├─┬ proxy-addr@1.1.2
│ ├── forwarded@0.1.0
│ └── ipaddr.js@1.1.1
├── qs@6.2.0
├── range-parser@1.2.0
├─┬ send@0.14.1
│ ├── destroy@1.0.4
│ ├─┬ http-errors@1.5.0
│ │ ├── inherits@2.0.1
│ │ └── setprototypeof@1.0.1
│ └── mime@1.3.4
├── serve-static@1.11.1
├─┬ type-is@1.6.13
│ └── media-typer@0.3.0
├── utils-merge@1.0.0
└── vary@1.1.0

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

{
  "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"]
}

Next, we will 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!');
});

You are able to test the code locally while still in development to ensure that it works like this:

$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 (When You Have No Internet Connection):

Make sure that you install Node.js. This is so that the Node executable is available. You should verify this once again by running “node -v” (as shown above).

Next, let's move the “guhelloproject-1.0.0.tgz” file to the Production server like the following:

$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!

Now, when you point your browser to http://:3000/, then you should see “Hello World”!

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
bundling ,express ,integration ,node

Published at DZone with permission of Venkatt Guhesan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}