paint-brush
Forgetting to set environment variables in nodejs appsby@meeshkan
217 reads

Forgetting to set environment variables in nodejs apps

by Meeshkan MLNovember 17th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

You’ve developed your killer <code class="markup--code markup--p-code">express</code> app. It’s passing unit tests, integration tests, end-to-end tests... You’re so content that you print the code out and pin it to your fridge. You deploy to production, but in a few hours, you get complaints that the service is more or less completely useless. You dig through debug logs, but to no avail — there’s nothing obviously wrong, and tests are passing, so your users must just be exaggerating. All of them.
featured image - Forgetting to set environment variables in nodejs apps
Meeshkan ML HackerNoon profile picture

tl;dr yarn add --dev faceplant

You’ve developed your killer express app. It’s passing unit tests, integration tests, end-to-end tests... You’re so content that you print the code out and pin it to your fridge. You deploy to production, but in a few hours, you get complaints that the service is more or less completely useless. You dig through debug logs, but to no avail — there’s nothing obviously wrong, and tests are passing, so your users must just be exaggerating. All of them.

Or maybe you forgot to set an environment variable in production.

From my experience, this is the #1 source of service failure in well-tested production apps, and this has reared it’s ugly head at Meeshkan as well.

To fix this, we made [faceplant](https://github.com/Meeshkan/faceplant), a TSLint plugin for production workloads. The goal is to replace start commands like ts-node dist/server.ts with things like tslint --project . && ts-node dist/server.ts so that a linting step is done on the production environment and the service will fail fast if the lint check doesn’t pass.

The first rule in this package, no-unset-env-variables, allows you to check to make sure that your env variables are set in both dev and production environments. There are options for reading from an .env file or using the actual environment. You can even have it check for variables not present in your code. All this adds up to safer production code and a more pleasant development experience.

If you’ve never developed a TSLint plugin before, it’s dead easy. We used this great boilerplate to get started and figured out how to make it production-ready by following the conventions in this repo.

Otherwise, when in doubt, use [http://i-forgot-to-set-my-env-variable.com](http://i-forgot-to-set-my-env-variable.com).

axios.get(process.env.API_URL || "http://i-forgot-to-set-my-env-variable.com")

Pull requests welcome!