Get and Set Environment Variables in Node.js

By  on  

One of the best ways to use sensitive information in open source repositories without hard-coding the information within publicly available repositories is setting environment variables.  Set the environment variables on the server, retrieve them by key within your application.

When using Node.js, you can retrieve environment variables by key from the process.env object:

var mode = process.env.mode; // 'PRODUCTION', for example

var apiKey = process.env.apiKey; // '38294729347392432'

There are time when you may want to set environment variables while you run your node app -- these are set temporarily while the process is still running.  A common case is simulating environment variables during testing.  You can temporarily set these variables by pegging items onto the process.env object:

process.env.mode = 'TESTING';

// Now app code knows not to do destructive transactions!

Simple enough but worth documenting for future use!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Using MooTools For Opacity

    Although it's possible to achieve opacity using CSS, the hacks involved aren't pretty. If you're using the MooTools JavaScript library, opacity is as easy as using an element's "set" method. The following MooTools snippet takes every image with the "opacity" class and sets...

  • By
    Retrieve Google Analytics Visits and PageViews with PHP

    Google Analytics is an outstanding website analytics tool that gives you way more information about your website than you probably need. Better to get more than you want than not enough, right? Anyways I check my website statistics more often than I should and...

Discussion

  1. I believe it’s more common to use process.env.NODE_ENV instead of process.env.mode

  2. This is an interesting problem. What if you are using aws or heroku for hosting? You could have your deployment script setup the keys, but you’d need to prompt each time you create a new instance.

    I encrypted the keys in a json file with AES 256 and in my run/deploy script it prompts for a password and decrypts it running the rest of the application with env variables. It’s not perfected yet, but this might be a good way to put it on github.

  3. Caleb

    Are these variables just being set via the command line?

  4. Ola

    I have had great success with json-configurator (https://www.npmjs.com/package/json-configurator). Use it with process.env.NODE_ENV as a input.

    require('json-configurator')(configJson, process.env.NODE_ENV ).userEndpoint;
    
  5. xgqfrms
    # Windows:
    
    SET NODE_ENV=development
    # or
    set NODE_ENV=development
    
    
    # OS X / Linux:
    
    export NODE_ENV=development
    
    
    
    # Windows CMD
    set NODE_ENV=production
    
    
    # Windows PowerShell:
    $env:NODE_ENV="production"
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!