Have production and development environment variables

This is how you would have production and development environment variables in a JavaScript nodejs app. I have a back-end nodejs app where I want to connect to a local MySQL server, and when deploying the app to Google Cloud App Engine; have my production settings instead.

First, try and see if you have already have set your enviroment to development:

console.log(process.env.NODE_ENV)

I get undefined, so what we want to do is setting the environment variable to dev when we are on our local computer, and we can do that by modifying our packages.json file where we run our app.

"start": "set NODE_ENV=dev&& node app.js"

Now when running npm start it prints out:
dev

Then download a module called dotenv.

npm i dotenv

Then create a config folder, and add two files: one for your production configuration called prod.env and one for your development configuration called dev.env. See this image with the red circle how my files are created in the folder.

Now I am going to define some properties I want to use for my MySQL connection. For my local development it is going to look like this.

HOST='localhost'
DBUSER='root'
PASSWORD=''
DATABASE='fileideadatabase'
SOCKETPATH=null

Then for my actual production environment configurations I am going to define different values along with SOCKETPATH since I am using Google Cloud SQL to connect from my app in the Google Cloud App Engine.

HOST='12.345.678.90'
DBUSER='root'
PASSWORD='myepicpassword'
DATABASE='fileideadatabase'
SOCKETPATH='/cloudsql/myinstancething:us-central1:fileideaapp'

(There are no commas here watch out writing those!). Okay now we got our environment configurations for both development and production. Let’s load these configurations based on what environment we are in.

if (process.env.NODE_ENV === 'dev') {
    require('dotenv').config({ path: 'config/dev.env' });
}
else {
    require('dotenv').config({ path: 'config/prod.env' });
}

The above code which will check if the environment variable (NODE_ENV) is having the value of Dev, which is has if we start the application locally with npm start which we made sure of in the beginning. The will then continue run the dotenv.config method and load the configuration from the pathconfig/dev.env”. That is the file from inside the folder we created earlier. Same procedure with production.

Most hosting services will change the enviroment variable to production for you so you don’t need to worry about it, for example, if we do: gcloud app deploy NODE_ENV will be set to production thus use the prod.env configurations.

Applying the configuration variables to a MySQL connection like this:

    var mysql = require('mysql');
    let sqlConfig = {
        host: process.env.HOST,
        user: process.env.DBUSER,
        password: process.env.PASSWORD,
        database: process.env.DATABASE
    };
    if (process.env.SOCKETPATH) {
        sqlConfig.socketPath = process.env.SOCKETPATH
    }
    var connection = mysql.createConnection(sqlConfig);

What is worth noting is that we will check if we got the SocketPath property (from the production enviroment variables) is defined or not and put it in to our object as well if it is defined.

Remember to not include your production values in any source control.

You can obviously also add more environments if you got stage etc here as well but this is for how I go about it for the dev and prod environment.

0 0 votes
Article rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments