Let's use different environment variables with ExpressJS TypeScript server

Let's use different environment variables with ExpressJS TypeScript server

When you developing your servers, do you have the need to have different values for certain configurations depending on the current environment: development, staging or production?

If the answer is YES, and you've never worked with environment variables before - this short tutorial is for you!

Let's assume that you already have a configured server with TypeScript.

Step 1: Add dotenv dependency

In this tutorial, we'll use the dotenv dependency which is a small module that loads .env files into process.env.

Add the dependency to your project

npm install dotenv --save

Step 2: Create env files

We need different configuration variables when configuring our servers. For example, you will need different addresses, ports, and so on for the databases or any other services, you will use.

In this tutorial, we will create two files:

  • .env.production - containing all the variables for the production configuration
  • .env.development - containing all of the variables for the development configuration

Let's assume that you're using Amazon's S3 service

Create the following two files:

S3_BUCKET="YOUR_DEV_S3BUCKET"
SECRET_KEY="YOUR_DEV_SECRETKEYGOESHERE"
.env.development
S3_BUCKET="YOUR_PROD_S3BUCKET"
SECRET_KEY="YOUR_PROD_SECRETKEYGOESHERE"
.env.production

Step 3: Create a config file

We will extract the loading of the env variables into a separate file:

import * as dotenv from "dotenv";

let path;
switch (process.env.NODE_ENV) {
    case "production":
        path = `${__dirname}/../.env.production`
        break;
    default:
        path = `${__dirname}/../.env.development`
}
dotenv.config({ path: path })

export default {
    S3_BUCKET: process.env.S3_BUCKET,
    SECRET_KEY: process.env.SECRET_KEY
}
src/config.ts

If you import this config into your app, you will notice that the env variables will not be found in the process.env. This is because TypeScript doesn't know about them.

Let's create a new file:

namespace NodeJS {
  interface ProcessEnv {
    S3_BUCKET: string
    SECRET_KEY: string
  }
}
src/types/global.d.ts

If you haven't mentioned the types directory in your TypeScript configuration, you will have to add it in the tsconfig.json in the typesRoot:

{
  "compilerOptions": {
    ...
    "typesRoot": ["./src/types"]
  }
}
tsconfig.json

Step 4: Use the env variables in your app

Include the config.ts in your server configuration file:

import config from './config'

And now you can use the env variables by calling:

  • config.S3_BUCKET
  • config.SECRET_KEY

Step 5: How to start the server in production mode

When you want to use your production configuration, you have to specify the NODE_ENV when starting the server.

For example:

NODE_ENV=production node src/app.js

Conclusion

This is a really small tutorial on how to use env variables for the different environments you want to run your servers.

Happy coding! 👨‍💻