Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Steve Withington
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Steve Withington ( @stevewithington )

Providing Environment Variables When Executing Node.js Program

Published in

Lately, I've been trying to wrap my head around Node.js streams by reading through the Node.js source code. In the latest branch, I noticed that there were a lot of calls to debug(). And, when I looked at the source code for debug(), I noticed that it only ran when the environmental variables dictated debugging. And, after some Googling, I found out that you could define just-in-time environmental variables when executing a Node.js program. I didn't know that this was a feature of Unix; so, I thought I would help spread the good news.

In order to provide just-in-time environmental variables, you can define key-value pairs on the command line before you invoke the executable:

key1=value1 key2=value2 your-executable

To experiment with this in Node.js, I created a JavaScript file that outputs an environmental variable and logs a debug message, using various methods, depending on the environment:

// I log information to the console, if debugging is enabled. This is expecting to
// use either the console.log or the console.trace method.
debug = process.env.debug
	? console[ process.env.debug ]
	: function() { /* no-op. */ }

// Test environmental variable.
console.log( "Env( hello ): %s", process.env.hello );

// Trying debugging some information (behavior will change based on environment).
debug( "This is me debugging!" );

Notice that the debug() method, in my example, is always called; but, its output and behavior depends on the environmental variables. Now, let's try to call this Node.js program with various just-in-time environmental variables:

bens-imac:env ben$ node test.js
Env( hello ): undefined
bens-imac:env ben$ hello=world node test.js
Env( hello ): world
bens-imac:env ben$ hello=world debug=log node test.js
Env( hello ): world
This is me debugging!
bens-imac:env ben$ hello=world debug=trace node test.js
Env( hello ): world
Trace: This is me debugging!
. . . at Object.<anonymous> (/Users/ben/Sites/
. . . at Module._compile (module.js:456:26)
. . . at Object.Module._extensions..js (module.js:474:10)
. . . at Module.load (module.js:356:32)
. . . at Function.Module._load (module.js:312:12)
. . . at Function.Module.runMain (module.js:497:10)
. . . at startup (node.js:119:16)
. . . at node.js:906:3

Notice that the key-value pairs, defined on the command line prior to the "node" executable, define just-in-time environmental variables.

This might be something that all of you command-line ninjas already knew. But, this seemed really cool to me. Plus, it will let me run those debug() methods in the Node.js source code when I upgrade to the latest branch.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel