Steve Bennett
Steve Bennett

Reputation: 126827

How to set environment variables using Node?

I am trying to automatically set three AWS environment variables (AWS_ACCESS_KEY, AWS_ACCESS_ID and AWS_SESSION_TOKEN) which are derived from the JSON returned by aws sts assume-role.

Normally if I wanted to automatically set environment variables I would write a Bash script, say setvars.sh:

export AWS_ACCESS_KEY=something

and then

source setvars.sh

I know that if you do process.env.AWS_ACCESS_KEY = 'something' in a Node script, it won't affect the parent process.

Is there a workaround to be able to use a Node script (rather than Bash, which will be tricky to manipulate JSON with) to set environment variables this way?

Upvotes: 1

Views: 3122

Answers (3)

Denis Tsoi
Denis Tsoi

Reputation: 10414

As an expansion to the accepted answer, You can also define your ENV in a file, and import a specific env config based on a release.

This way you can set ENV files for your staging, development or production environments in static configs if required.

From dotenv repository/docs

As early as possible in your application, require and configure dotenv.

require('dotenv').config()

Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

process.env now has the keys and values you defined in your .env file.

const db = require('db')
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
})

So for you, you may want to do an async call from AWS to get those ENV variables, or perhaps save them within a .env file

https://github.com/motdotla/dotenv

Upvotes: 0

Vishal-L
Vishal-L

Reputation: 1347

When you set environment variables using a bash script and run in a shell, they are accessible just to the processes which are run in the same shell. So you will need to run the Node app in the same shell to access those variables.

Now another approach is to add them in process.env object, so you can write a config script just to load all the config variables and require it at top of your Node app. You need to design the application in such a way that you can use all the configuration in same file.

For Example:

config.js:

process.env.AWS_ACCESS_KEY = 'something'

app.js:

// Starting point of your app
require('./config');
const app = require('express')();

// Use the config
// AWS_API(process.env.AWS_ACCESS_KEY);

// Other App Logic

This approach is mostly used in development environment, in production you might want to use the first approach or you can add the configuration globally using /etc/profile or /etc/environment. Refer how-to-set-global-environment-variable

Upvotes: 2

PrivateOmega
PrivateOmega

Reputation: 2900

You can read the JSON file and set your environment variables in process.env:

process.env['environment_variable_name'] = 'environment_variable_value';

NB: This will be available only for that particular node process and its children, not globally available.

Upvotes: 0

Related Questions