Setup environment variables?

5,618 views
Skip to first unread message

Guy Halford-Thompson

unread,
Mar 24, 2011, 12:15:52 PM3/24/11
to nod...@googlegroups.com
What is the best way to setup environment variables (e.g. DEVELOPMENT,
PRODUCTION, TESTING) in node?

I use different ports and database info for test/production servers
and was wondering what the best way to make this a bit more automated
is.

Thanks

--
Guy Halford-Thompson - http://www.cach.me/blog

Aria Stewart

unread,
Mar 24, 2011, 12:18:05 PM3/24/11
to nod...@googlegroups.com
On Thursday, March 24, 2011 at 10:15 AM, Guy Halford-Thompson wrote:
What is the best way to setup environment variables (e.g. DEVELOPMENT,
> PRODUCTION, TESTING) in node?
>
> I use different ports and database info for test/production servers
> and was wondering what the best way to make this a bit more automated
> is.

Look at process.env?
----
Aria Stewart


Guy Halford-Thompson

unread,
Mar 24, 2011, 12:28:29 PM3/24/11
to nod...@googlegroups.com
I have, but there is only a couple of lines of documentation... could
someone point me to an example?

Ty

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Matt

unread,
Mar 24, 2011, 12:58:16 PM3/24/11
to nod...@googlegroups.com, Guy Halford-Thompson
if (process.env.DEVELOPMENT) {
// setup devel stuff
}
else if (process.env.TESTING) {
// setup testing stuff
}
else {
// assume production
}

then start in bash as: DEVELOPMENT=1 node code.js

mscdex

unread,
Mar 24, 2011, 12:59:13 PM3/24/11
to nodejs
On Mar 24, 12:28 pm, Guy Halford-Thompson <g...@cach.me> wrote:
> I have, but there is only a couple of lines of documentation... could
> someone point me to an example?

It's just a plain js object:

process.env['JOBS'] = 8; // Set
console.log(process.env['JOBS']); // Get

Guy Halford-Thompson

unread,
Mar 24, 2011, 1:33:00 PM3/24/11
to nod...@googlegroups.com
Sorry, I should have been a bit more specific in my question... I was
wondering how to set the variables on the server, not access them in
node

I understand how you can access
....
process.env['JOBS']
...

but how do I set the variable 'JOBS' on my server? This cannot be
done in my node files as they will be the same on all servers.

Thanks for the help

G

Corey Jewett

unread,
Mar 24, 2011, 1:59:08 PM3/24/11
to nod...@googlegroups.com
On Mar 24, 2011, at 10:33 , Guy Halford-Thompson wrote:

> Sorry, I should have been a bit more specific in my question... I was
> wondering how to set the variables on the server, not access them in
> node
>
> I understand how you can access
> ....
> process.env['JOBS']
> ...
>
> but how do I set the variable 'JOBS' on my server? This cannot be
> done in my node files as they will be the same on all servers.

Depends on your shell's syntax for environment variables (google
that), but typically:

$> JOBS=123
$> export JOBS
$> node
> process.env["JOBS"]
'123'

Corey

Isaac Schlueter

unread,
Mar 24, 2011, 2:05:35 PM3/24/11
to nod...@googlegroups.com
Matt answered your question above.


Let's say you have this in your server.js file:

if (process.env.SERVER === "PRODUCTION") { .. produce stuff .. }


There are many ways to set environment variables.

You can set them on the command line for just one command:

$ SERVER=PRODUCTION node server.js

You can export them into your shell environment:

$ export SERVER=PRODUCTION
$ node server.js

You can export them from a shell script:

# foo.sh
export SERVER=PRODUCTION

$ source foo.sh # not ./foo.sh! that would run in a subshell
$ node server.js

You can pass a key-value list to the "env" command:

$ echo SERVER=PRODUCTION >> server-profile
$ echo FOO=bar >> server-profile
$ env $(cat server-profile) node server.js

If you're starting your program with node's child_process.spawn, you
can pass them on the options object:

child_process.spawn("node", ["server.js"], { env: { SERVER: "PRODUCTION" }})

If you're starting your program with execp in C, you can use the
"environ" extern.

extern char **environ;
int main (int argc, char** argv) {
char **saved_env;
int ret;
saved_env = environ;
environ = { "SERVER=PRODUCTION" };
ret = execvp("node", { "server.js" });
environ = saved_env;
return ret;
}

If you're loading your program from your bash login shell, you can put
this in your ~/.bashrc:

export SERVER=PRODUCTION

If you're running it with SMF, you can put this in your manifest xml:
<method_environment>
<envvar name="SERVER" value="PRODUCTION"/>
</method_environment>


There are probably other ways to set environment vars. "man sh" and
"man bash" have sections on this.

Environs are a pretty core Unix feature. Think of it like another set
of arguments to your program.

--i

Matt

unread,
Mar 24, 2011, 2:45:30 PM3/24/11
to nod...@googlegroups.com, Guy Halford-Thompson
It's pretty basic unix knowledge.

On Thu, Mar 24, 2011 at 2:18 PM, Guy Halford-Thompson <g...@cach.me> wrote:
Hi Isaac,

Thanks for your detailed response, that makes it very clear. Would it
be worth adding this to the documentation at some point? Or is it just
be being v inexperienced...

Thanks also to everyone else for you help.

Guy

Guy Halford-Thompson

unread,
Mar 24, 2011, 2:18:32 PM3/24/11
to nod...@googlegroups.com
Hi Isaac,

Thanks for your detailed response, that makes it very clear. Would it
be worth adding this to the documentation at some point? Or is it just
be being v inexperienced...

Thanks also to everyone else for you help.

Guy

Dhirender Tyagi

unread,
Mar 17, 2015, 1:06:38 PM3/17/15
to nod...@googlegroups.com, g...@cach.me
I think you should use foreman npm. Where you have to create a .env file to create your environment varible. 
Reply all
Reply to author
Forward
0 new messages