Setting Environment Variables for Node to retrieve

user1107173 picture user1107173 · Mar 10, 2014 · Viewed 654.2k times · Source

I'm trying to follow a tutorial and it says:

There are a few ways to load credentials.

  1. Loaded from environment variables,
  2. Loaded from a JSON file on disk,

The keys need to be as follows:

USER_ID, USER_KEY

...This means that if you properly set your environment variables, you do not need to manage credentials in your application at all.

Based on some Googling, it appears that I need to set the variables in process.env? How and where do I set these credentials? Example Please.

Answer

SamT picture SamT · Mar 10, 2014

Environment variables (in this case) are being used to pass credentials to your application. USER_ID and USER_KEY can both be accessed from process.env.USER_ID and process.env.USER_KEY respectively. You don't need to edit them, just access their contents.

It looks like they are simply giving you the choice between loading your USER_ID and USER_KEY from either process.env or some specificed file on disk.

Now, the magic happens when you run the application.

USER_ID=239482 USER_KEY=foobar node app.js

That will pass the user id 239482 and the user key as foobar. This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables.