I\'m trying to follow a tutorial and it says:
There are a few ways to load credentials.
- Loaded from environment variables,
You can set the environment variable through process global variable as follows:
process.env['NODE_ENV'] = 'production';
Works in all platforms.
Step 1: Add your environment variables to their appropriate file. For example, your staging environment could be called .env.staging
, which contains the environment variables USER_ID
and USER_KEY
, specific to your staging environment.
Step 2: In your package.json
file, add the following:
"scripts": {
"build": "sh -ac '. ./.env.${REACT_APP_ENV}; react-scripts build'",
"build:staging": "REACT_APP_ENV=staging npm run build",
"build:production": "REACT_APP_ENV=production npm run build",
...
}
then call it in your deploy script like this:
npm run build:staging
Super simple set up and works like a charm!
Source: https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d
Came across a nice tool for doing this.
node-env-file
Parses and loads environment files (containing ENV variable exports) into Node.js environment, i.e. process.env
- Uses this style:
.env
# some env variables
FOO=foo1
BAR=bar1
BAZ=1
QUX=
# QUUX=
As expansion of @ctrlplusb answer,
I would suggest you to also take a look to the env-dot-prop package.
It allows you to set/get properties from process.env
using a dot-path.
Let's assume that your process.env
contains the following:
process.env = {
FOO_BAR: 'baz'
'FOO_
If you want a management option, try the envs npm package. It returns environment values if they are set. Otherwise, you can specify a default value that is stored in a global defaults object variable if it is not in your environment.
Using .env ("dot ee-en-vee") or environment files is good for many reasons. Individuals may manage their own configs. You can deploy different environments (dev, stage, prod) to cloud services with their own environment settings. And you can set sensible defaults.
Inside your .env
file each line is an entry, like this example:
NODE_ENV=development
API_URL=http://api.domain.com
TRANSLATION_API_URL=/translations/
GA_UA=987654321-0
NEW_RELIC_KEY=hi-mom
SOME_TOKEN=asdfasdfasdf
SOME_OTHER_TOKEN=zxcvzxcvzxcv
You should not include the .env
in your version control repository (add it to your .gitignore
file).
To get variables from the .env
file into your environment, you can use a bash script to do the equivalent of export NODE_ENV=development
right before you start your application.
#!/bin/bash
while read line; do export "$line";
done <source .env
Then this goes in your application javascript:
var envs = require('envs');
// If NODE_ENV is not set,
// then this application will assume it's prod by default.
app.set('environment', envs('NODE_ENV', 'production'));
// Usage examples:
app.set('ga_account', envs('GA_UA'));
app.set('nr_browser_key', envs('NEW_RELIC_BROWSER_KEY'));
app.set('other', envs('SOME_OTHER_TOKEN));
Windows-users: pay attention! These commands are recommended for Unix but on Windows they are only temporary. They set a variable for the current shell only, as soon as you restart your machine or start a new terminal shell, they will be gone.
SET TEST="hello world"
$env:TEST = "hello world"
To set a persistent environment variable on Windows you must instead use one of the following approaches:
A) .env file in your project - this is the best method because it will mean your can move your project to other systems without having to set up your environment vars on that system beore you can run your code.
Create an .env
file in your project folder root with the content: TEST="hello world"
Write some node code that will read that file. I suggest installing dotenv ( npm install dotenv --save
) and then add require('dotenv').config();
during your node setup code.
process.env.TEST
Env-files are a good of keeping api-keys and other secrets that you do not want to have in your code-base. Just make sure to add it to your .gitignore
.
B) Use Powershell - this will create a variable that will be accessible in other terminals. But beware, the variable will be lost after you restart your computer.
[Environment]::SetEnvironmentVariable("TEST", "hello world", "User")
This method is widely recommended on Windows forums, but I don't think people are aware that the variable doesn't persist after a system restart....
C) Use the Windows GUI