Setting Local environment variables for development
You may wish to override the default settings for Voluntarily e.g for the location of the database, port and url address used etc.
The Voluntarily application gets all its settings from config.js
This initialises and exports a config structure like this:
"config": { "appName": "Voluntarily NZ", "serverPort": 3122, "databaseUrl": "mongodb://localhost/vly2", "jsonOptions": { "headers": { "Accept": "application/json", "Content-Type": "application/json" } }, "auth": { "AUTH0_CLIENT_ID": "S4yd4VgZ92NIjhwO3vt4h0Gifb9mXv1k", "AUTH0_CLIENT_DOMAIN": "voluntarily.au.auth0.com" }, "apiVersion": "v1", "SMTP_ID": "", "SMTP_PWD": "", "onlyEmailText": false, "appUrl": "http://localhost:3122" }
These are either hard coded or initialised from the Process.env environment variables with
a default value if not set in the env.
e.g databaseUrl: process.env.MONGODB_URI || `mongodb://localhost/${databaseName}
If you need to override any values for your local environment you can do so in the following ways:
Set on the command line
This is good for temporary one off calls
PORT=3123 npm run dev
Export into your environment
This will persist as long as your terminal window is open.
export PORT=3123
To undo use 'unset PORT'
Use a .env file.
Using a .env file is the easiest and safest way to setup a local environment.
PORT=80 APP_URL=http://vly.myserver.local MONGODB_URI=mongodb+srv://vly-client:AAAAAAAAAAAAAA@cluster0-kwmsu.mongodb.net/vly2-avw?retryWrites=true' NODE_ENV=production
YOU MUST NOT COMMIT YOUR .ENV FILE TO THE GITHUB REPOSITORY AS THIS WILL POLLUTE EVERYONE ELSE'S ENVIRONMENT AND MAY RESULT IN THE PUBLISHING OF PRIVATE API KEYS.
List of Env Vars used
Var | Description | Default | Usage |
---|---|---|---|
APP_URL | The full hostname and port of the application. | https://localhost:3122 | Used in reduxApi and apiCaller to tell the client side code the entry point for api calls. Used in outgoing emails to provide links back to the application |
PORT | Port server will listen on | 3122 (dev) 8080 (test) | Used in server.js as the port the server listens on. test uses a different port so it doesn't conflict with a locally running app |
NODE_ENV | Tells express the node environment | development | set to 'test' for npm test, set to 'production' in production. Mainly used to comment out test or dev specific output Express uses NODE_ENV to alter its own default behavior. For example, in development mode, the default error handler will send back a stacktrace to the browser. In production mode, the response is simply |
SMTP_ID SMTP_PWD | Settings for the email SMTP server connection. AWS Specific | unset - must be set by user | used in email.js to init the AWS SES transport layer. |
TEXT_ONLY_EMAIL | sets emails to send text only - no images | true | see emailperson.js |
Conclusion
Environment variables exist outside our application's code, they are available where our application is running. They can be used to decouple our application's configuration from its code, which allows our apps to be easily deployed across different environments.
With Node.js apps, environment variables are available through the process.env
global variable. We can set the environment variables before we run the node
command, or we can use the dotenv
library which allows us to define our environment variables in a .env
file.
The .env
file should never be in the source code repository.