user1107173 user1107173 - 1 year ago 74
Node.js Question

Node.js: Setting Environment Variables

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:


...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
? How and Where do I set these credentials? Example Please.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download