Read Environment Variables in Node.Js

Read environment variables in Node.js

process.env.ENV_VARIABLE

Where ENV_VARIABLE is the name of the variable you wish to access.

See Node.js docs for process.env.

Setting Environment Variables for Node to retrieve

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.

Use node.js commandline argument as environment variable

When doing node index.js a=5, a=5 is an argument for node, as index.js is.

If you want to pass an environment variable, you must specify it before node command : a=5 node index.js.

The node process.env is populated with your bash environment variables.

How to get Digitalocean environment variable?

I would suggest using an environment function in your language of choice that searches the .env file or current global env and pull the variable that is available.

Example: for R, I can use Sys.getenv("MY_SECRET") to pull the variable from DO.



Related Topics



Leave a reply



Submit