Node.js application works locally but not in Cloud

My app.js file looks like this:

var port = (process.env.VMC_APP_PORT || 3000);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var http = require('http');
var rip = require('./config.js');

http.createServer(function (req, res) {
        //do nothing...no response expected via http
}).listen(port, host);

The config.js file contains this (and more, but cut for brevity/relevance):

//several global.<property_name> assignments
global.mName                    = "Application Name";
//serveral more global.<property_name> assignments
...
...
require("./main.js");

The main.js file begins like this:

console.log(mName+" >>> Loading.");

When I run the application locally, it executes fine by running:

node app.js

However, when I push the application to cloud foundry using the following command:

vmc push [app_name] --runtime=node06

I get the following error:

Creating Application: OK Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (10K): OK
Push Status: OK Staging Application 'app_name': OK
Starting Application 'app_name': ..........................

Error: Application [app_name] failed to start, logs information below.

====> /logs/stderr.log <====

node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ ReferenceError: mName is not defined at Object. (/var/vcap/data/dea/apps/app_name-0-6996d737ed319bcffaf696d653423d7c/app/main.js:6:1) at Module._compile (module.js:411:26) at Object..js (module.js:417:10) at Module.load (module.js:343:31) at Function._load (module.js:302:12) at Array. (module.js:430:10) at EventEmitter._tickCallback (node.js:126:26)

It seems like the properties I set in the global object in config.js are not being carried to the main.js. I verified this by modifying the main.js to explicitly reference global.mName rather than just mName and that error went away, but another reference to a global object property then started to complain.

Since everything works fine locally without the explicit "global." reference, I don't think I should have to explicitly reference "global." for those variables. Is there something I'm missing here? Any help is appreciated.

Thanks!

Go ahead and add the following package.json file to the root of your application directory:

{
  "name":"hello-node",
  "version":"0.0.1",
  "scripts": {
    "start": "app.js"
  }
}

Now what the package.json is doing for you in this case is, it's telling cloudfoundry.com that your execution/start script file should be "app.js"

The reason you need to have this in place is because you also have a "main.js" file. In cloudfoundry.com, by default the main.js file is over writing your app.js so its breaking your work.

So the package.json with the scripts object in it fixes the problem :-)