Deploy Node.js apps

Clever Cloud allows you to deploy any Node.js application. This page will explain you how to set up your application to run it on our service.

You do not need to change a lot in your application, the requierments will help you to configure your apps with some mandatory files to add, and properties to setup.

Overview

Node.js is a platform built on Chrome’s JavaScript runtime for building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, pretty good for data-intensive real-time applications that run across distributed devices.

Create an application

  1. Create a new app by clicking on the Add an App button, in the headbar.
  2. Enter your application’s name and description and click “Next”.
  3. Then select the language/framework:
  4. Optional: in case of PHP applications, you can choose between FTP and Git deployment
  5. Check that the information are correct and validate:
  6. Optional: add an add-on

Necessary information

Be sure that:

  • you have pushed in master branch
  • you listen on port 8080
  • you have added application name in package.json
  • you have added application version in package.json
  • you have added application start script in package.json

Requirements

First, your application must be set to listen on the 8080 port, for worldwide connections. The following code describes a Hello world application listening on that port:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Last, but not least, listen on port 8080
server.listen(8080);

Then, a package.json file is mandatory to initiate your app deployement on Clever Cloud. The next section will detail this point.

Describing package.json

Even if you have no dependencies, you have to provide a package.json file at the root of your project’s directory.

About depedencies:
  • For every Node.js project you HAVE TO provide a package.json file at the root of your project’s directory.
  • Additionally, make sure that the folder “/node_modules” is in your .gitignore file before pushing your app.

If you already are a Node.js guru, you probably won’t have to change anything to that file. Just check the required fields below.

The package.json file should look like the following:

{
  "name" : "myapp",
  "version" : "0.1.0",
  "main" : "myapp.js",
  "scripts" : {
    "start" : "node myapp.js"
  },
  "engines" : {
    "node" : ">=0.6"
  }
}

The required fields

The following table describes each of the fields formerly mentioned.

Usage Field Description
Required name Name of your application. You need to fill this field.
Required version Version of you application. You need to fill this field.
At least one main This field allows you to specify the file you want to run. It should be the relative path of the file starting at the project’s root. It’s used prior to the next one.
scripts.start This field provides a command line to run. It is required if the main one is missing. You need one of scripts.start and main. If both exist, we use the main one.
Optional engines.node Sets the node engine version you app runs with. Any “>=” version will lead to run the application with the latest local version. Any “A.B.x” version will lead to run the application with le latest “A.B” local version. If this field is missing, we use the greatest local version.

NPM modules dependencies

If you need some modules you can easily add some with the dependencies field.

Here is an example :

{
  "name" : "myapp",
  "version" : "0.1.0",
  "main" : "myapp.js",
  "scripts" : {
    "start" : "node myapp.js"
  },
  "engines": {
    "node": "0.8.15"
 },
"dependencies": {
  "express": "3.x",
  "socket.io": "0.9.x",
  "underscore": "1.4.3"
  }
}

Node.js supported versions

The Clever Cloud can virtually run any version of node >= 0.6 and any module. Lesser (pre-npm) versions are not officially supported. Unstable versions are not supported either.

Pre-installed modules

We currently host the following Node.js versions, with these modules already installed:

You can use any version of node you need and any modules you need. Preinstall version is only for faster deployment process.

Pre-installed modules
socket.io
express
async
mysql
pg

New versions will be added as they go out.

Defining pre-installed

The above table describes the modules pre-installed. These modules are available at deploy time, without the need to download and install them.

If you use modules that are not pre-installed, we will just get them with npm (provided they are in the npm repository), and install it before we start your application. The deploy will then be a little longer, due to probable compilation of some of these modules.

Some modules require system dependencies to build, require to rebuild node… so we installed the most used ones.

If you think more modules are commonly used and should be pre-installed, do not hesitate to contact us at

Environment injection

Clever Cloud can inject environment variables that are defined in the dashboard and by add-ons linked to your application.

To access your variable in your application, nothing simpler! Just get it from your environment, like you would with PATH: process.env["MY_VARIABLE"].

Deploy on Clever Cloud

Application deployment on Clever Cloud is via Git. Follow these steps to deploy your application.

Deployment video

More info on nodejs.org.


Write a Tutorial and Get Rewarded. Learn More