2

I have:

  • added export NODE_ENV=production to app-root/data/.bashprofile (and echo $NODE_ENV shows production)
  • Tried rhc set-env NODE_ENV=production -a myapp which generates Setting environment variable(s) ... Server returned an unexpected error code: 501
  • Created an .openshift/action_hooks directory in my local repo and added the line export NODE_ENV=production to both a start and a pre_start file, committed and pushed to openshift
  • Tried the .openshift/action_hooks/pre_start_* aswell
  • Declared a root variable (as tipsed by @JuJoDi below) in the beginning of my server.js file, now it looks lite this:

    var express = require('express'),
     routes = require('./routes'),
     api = require('./routes/api'),
     http = require('http'), 
     path = require('path'),
     everyauth = require('everyauth'), 
     connect = require('connect'),
     env = process.env.NODE_ENV ? process.env.NODE_ENV : "process.env.NODE_ENV is null";
    

I'm then printing out using this route:

app.get('/version', function(req,res){
        res.json({
            "app.get('env')" : app.get('env'),
            "process.env.NODE_ENV" : process.env.NODE_ENV,
                            "Global env" : env
        });
    });

What happens is that app.get('env') always returns 'development' and process.env.NODE_ENV is not printed at all (null value)

Has anyone got any ideas why this isn't working?

UPDATE

I created a minimalistic node server, still can't get any env-variables to work on open shift:

    var http = require('http');

    var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
    var ipaddress = process.env.OPENSHIFT_NODEJS_IP || process.env.OPENSHIFT_INTERNAL_IP || 'localhost';
    var env = process.env.NODE_ENV

    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('' + process.env.NODE_ENV + "|" + env);
    }).listen(port, ipaddress);
Joche
  • 346
  • 7
  • 17

3 Answers3

3

I couldn't reproduce this issue. I think the 501 error response that you initially received is the cause of your problems.

You should be able to troubleshoot by running the following from within in your project source folder (add -a YOUR_APP_NAME to the end of each command to run elsewhere):

  1. Check your current list of application tokens:

    rhc env list

  2. If you have something listed for NODE_ENV, then clear it:

    rhc unset NODE_ENV

  3. Set your NODE_ENV to "production":

    rhc env set NODE_ENV="production"

  4. Verify that the value has been set by reloading your server, by running rhc env list, or by connecting to your application over SSH to check the system environment directly:

    rhc ssh

    env | grep NODE_ENV

Running rhc help env provides a lot of good usage info as well. Depending on how they're written, some servers need to be reloaded in order to fetch the new content.

I think the easiest way to get up and running with MEANStack on OpenShift is to use Yeoman's Angular-fullstack generator. It should automatically configure your environment for you.

ʀɣαɳĵ
  • 1,992
  • 1
  • 15
  • 20
  • Happy to help troubleshoot in #OpenShift on FreeNode IRC as well – ʀɣαɳĵ May 13 '14 at 20:11
  • 1
    Well, today when I revisit this issue and followed your example it worked very well. I don't know if I have changed anything or if something has happened on openshift. However, thank you very much! – Joche May 14 '14 at 18:27
0

That closure does not have access to the process object. Above that try

var env = process.env.NODE_ENV;

then print it with

app.get('/version', function(req,res){
        res.json({
            "app.get('env')" : app.get('env'),
            "process.env.NODE_ENV" : env
        });
    });
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • Thanks for trying to help. I tried out your idea but it still don't work. On my local node.js server the closure has access to the process even in that scope. – Joche May 01 '14 at 12:34
  • Interesting, I just tested it on mine and it definitely does not. It's a `var self = this` situation. What version of node are you running? – JuJoDi May 01 '14 at 12:40
  • I'm running node.js v0.10.12 – Joche May 01 '14 at 12:42
0

When you run the app, try this:

NODE_ENV=production npm start

Substitute npm start with whatever way you start your app (node server or whatever).

That's the way I change the environment, when I am working locally. Since I usually want it to be development.

Express.js will default app.get('env') to development if process.env.NODE_ENV isn't set.

Update:

Sorry, missed the OpenShift part :/

https://www.openshift.com/developers/openshift-environment-variables

Looks like the environment variables in OpenShift need to go in:

.openshift/action_hooks/build

Using export. Have you tried that?

Johnny Hall
  • 545
  • 4
  • 12
  • Thank you Johnny, that works great on my local machine. Actually I have no problems locally just with the OpenShift environment. I simply can't get any custom environmental variables to work when deploying on openshift. – Joche May 01 '14 at 13:04