Don’t use NODE_ENV to specify your environment

Josh Woodcock
2 min readJul 15, 2020

--

I see so many times developers are using NODE_ENV to specify their environment. There’s 1 particular reason you might not want to do that.

In both npm and yarn, when you specify NODE_ENV=production then the devDependencies in the package.json are skipped. Fantastic right?

The next thing I see though is a config file that looks something like this:

const configs = {
production: { dbName: "productiondb", user: "my-user", password: "mypassword"},
development: { dbName: "devdb", user: "my-user", password: "mydevpassword"}
}const config = configs[process.env.NODE_ENV]

Perfect! You say what’s wrong!?

Well suppose I want to have a production-like environment. Like let’s say for training.

const configs = {
production: { dbName: "productiondb", user: "my-user", password: "mypassword"},
development: { dbName: "devdb", user: "my-user", password: "mydevpassword"},
training: { dbName: "traindb", user: "my-user", password: "mytrainpassword"}
}
const config = configs[process.env.NODE_ENV]

Well now my NODE_ENVwill be "training" which means the devDependencies will be installed and it is more like development than it is like production .

There’s actually quite a few other applications that also use different configuration settings when NODE_ENV is = production including the actual node process being more performant. Just another reason not to use it for specifying the canonical environment name.

So what’s the solution then?

The solution would be to set NODE_ENV to either production or development and use another variable like ENV_NAME to differentiate configurations.

const configs = {
production: { dbName: "productiondb", user: "my-user", password: "mypassword"},
development: { dbName: "devdb", user: "my-user", password: "mydevpassword"},
training: { dbName: "traindb", user: "my-user", password: "mytrainpassword"},
}
const config = configs[process.env.ENV_NAME]

This way NODE_ENV=production and ENV_NAME=training . NODE_ENV should be used to specify a category of environment not the actual environment name.

--

--

Josh Woodcock
Josh Woodcock

No responses yet