Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different env variables for testing and development in nodejs

I'm developing a nodejs api in my free time, and I'm trying to implement testing now. I'm currently loading my environment variables from a .env file (loaded using dotenv), which include the DB_URI, DB_USER and DB_PASSWORD for my development mongodb database.

Now, I would like to create a separate database for testing however I don't know how would I load different variables to connect to the testing database instead of the development database. I deploy to Heroku where I have different environment variables so that's covered just fine.

I've tried to find online for some answers for best practices, but I have been unable to. I thought of creating a different .env file, however that's not recommended according to the documentation on npmjs..

Other resources recommended recommended hard coding the specific variables I needed in the package.json script. However, the script would be huge if I had to change all the variables needed to connect to a different database.

Can I get some help understanding how I should do this?

Thanks!

PS: In case it's needed, I'm using mocha and supertest for my tests.

like image 456
Andreas L Orozco Avatar asked Jan 25 '18 01:01

Andreas L Orozco


People also ask

What are environment variables in node JS?

Environment variables provide information about the environment in which the process is running. We use Node environment variables to handle sensitive data like passwords, which we shouldn't hard code, or configuration details that might change between runs, like what port a server should listen on. In Node.

Should I have multiple .env files?

There is a risk that your production database could be deleted if you store different connection URLs to each of your environments within a single . env file. One solution is to have multiple . env files which each represent different environments.


1 Answers

You can use the dotenv package as follows:

  1. In your .env file, add variables for each environment:

    DB_URI_DEVELOPMENT="https://someuri.com"
    DB_USER_DEVELOPMENT=someuser
    DB_PASSWORD_DEVELOPMENT=somepassword
    
    DB_URI_TEST="https://otheruri.com"
    DB_USER_TEST=otheruser
    DB_PASSWORD_TEST=otherpassword
    
  2. Start the application in development:

     NODE_ENV=development node server.js
    

    or in test:

     NODE_ENV=test node server.js
    
  3. Access the environment variables in your app:

    /**
     * This `if` block prevents loading of the .env file on Heroku by calling
     * dotenv.config() if and only if `NODE_ENV` is not equal to "production"
     *  
     * In order to set environment variables on Heroku, use "config vars":
     * @see {@link https://devcenter.heroku.com/articles/config-vars}.
     *
     * If you must use `dotenv` to load an .env file on Heroku, follow:
     * @see {@link https://stackoverflow.com/a/54884602/1526037}.
     */
    if (process.env.NODE_ENV !== 'production') {
      require('dotenv').config();
    }
    
    // Get the current environment, and convert to uppercase (e.g. "PRODUCTION").
    const env = process.env.NODE_ENV.toUpperCase();
    
    // Access the environment variables for the current environment
    // by postfixing them with the uppercase environment string.
    const {
      [`DB_URI_${env}`]: dbUri,
      [`DB_USER_${env}`]: dbUser,
      [`DB_PASSWORD_${env}`]: dbPassword,
    } = process.env;
    
    /*
     * Note, the above is the same as:
     * ---------------------------------------------------------
     * var dbUri = process.env['DB_URI_' + env];
     * var dbUser = process.env['DB_USER_' + env];
     * var dbPassword = process.env['DB_PASSWORD_' + env];
     */
    
like image 50
sbolel Avatar answered Sep 20 '22 05:09

sbolel