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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Full-Stack Resume Screening Application With AI
  • Node.js Walkthrough: Build a Simple Event-Driven Application With Kafka
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • How To Use the Node Docker Official Image

Trending

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • How to Hide Sensitive Information Before You Share a File
  • Spring Security JWT Validation Best Practices
  • Build a GitHub Contribution City With Java and Quarkus

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. Package Your Node.js Application for Deployment in an Offline Environment

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.

By 
Venkatt Guhesan
·
Oct. 18, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
45.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
└─┬ [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 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”!

Node.js application

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

Opinions expressed by DZone contributors are their own.

Related

  • Building a Full-Stack Resume Screening Application With AI
  • Node.js Walkthrough: Build a Simple Event-Driven Application With Kafka
  • Node.js: Architectural Gems and Best Practices for Developers to Excel
  • How To Use the Node Docker Official Image

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook