Meeshkan ML

@meeshkan

Forgetting to set environment variables in nodejs apps

November 17th 2018

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, 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.

Ah, linting. Saving Meeshkan’s production environment since 2018.

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.

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

Pull requests welcome!

More by Meeshkan ML

More Related Stories