OK, sure there is a GUI in Windows for this – in Advanced – Environment Variables accessed via the System control panel app.
But. a) where is the fun in that? And b) if you’re doing some development, switching to various different interfaces, keeping control panel apps open etc is a bit of a bore as well as being incredibly inefficient.
During development such as Node.js development, you’re probably going to want to tinker with the environment variables reasonably regularly.
What is the top change that a Node developer will want to make during development to these variables? Answer: switching debugging on (and then off) again.
Ever spun up a server in Node and something’s gone awry? Then you might want to debug the HTTP library requests.
The examples below are setting environment variables relating to Node.js, but the commands can be used to set up – or indeed create – any environment variables of your choosing.
The Server Example
The famous server example, that has pride and place in Node’s own documentation, is what we will be looking at today. Well, at least a variant of it – there are a few after all.
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello World. This is David!\n');
});
server.listen(4242, () => {
console.log('Server is running...');
});
This is in a simple JavaScript file called ‘hello-world.js’.
So, if we run this without debug mode on (default), the output that we get in the console is:
PS H:\NodeCode> node hello-world
Server is running...
Open a browser, head to localhost:4242 and here’s what you should get:
So, everything in this case is peachy, but what happens if it’s not?
Setting An Environment Variable
As previously mentioned, this example is the context of Node.js development – but can apply to any Environment Variable that you want to set for any reason what so ever.
We’re wanting to switch on debugging for the HTTP library specifically so, on the command line:
PS H:\NodeCode> $env:NODE_DEBUG="http"
Simple as that!
Stop the server executing (CTRL + C twice) in the console and restart again using the previously used command. Visit the web page in your browser – localhost:4242 and take a look at the different output:
PS H:\NodeCode> node 1-hello-world
Server is running...
HTTP 15920: SERVER new http connection
HTTP 15920: SERVER new http connection
HTTP 15920: write ret = true
HTTP 15920: outgoing message end.
HTTP 15920: SERVER socketOnParserExecute 828
HTTP 15920: resetHeadersTimeoutOnReqEnd
HTTP 15920: write ret = true
HTTP 15920: outgoing message end.
HTTP 15920: SERVER socketOnParserExecute 749
HTTP 15920: resetHeadersTimeoutOnReqEnd
Clearing an Environment Variable
Again, our example is in the context of Node.js – but it’s the same for clearing any environment variable.
Head back to the command line and:
PS H:\NodeCode> $env:NODE_DEBUG=""
We’re back to where we started. Good job!
Old School Command Line
Are you on an older version of Windows? Maybe you’re just stuck using the old school command line? If so, please do start using PowerShell.
But, if you do insist, you’ll have to tweak the call setting the environment variable so that it works on this ye olde setup:
H:\NodeCode>set NODE_DEBUG="http"