Multiple Environments in Node.js

nonsensetwice
{nonsensecodes}
Published in
3 min readAug 21, 2020

--

Across documentation and various articles, I’m trying to figure out if I can run a development and production environment side by side in Node.js.

It is my understanding that setting up different types of environments in Node.js is not only possible, but encouraged. Through conscious use of devDependencies, dependencies, and the environment variable, setting the parameters for each environment is as simple as defining each configuration’s variables and then writing the logic for the app to determine what set of variables to use for what environment:

// example config.json{
”development”: {
“dbURL”: dev database,
”dbUSER”: dev user,
”dbPW”: dev user password
},
”production”: {
“dbURL”: prod database,
”dbUSER”: prod user,
”dbPW”: prod user password
}
}

and to add to the main app file:

import config from ’./config.json’;const env = process.env.NODE_ENV || ’development’;
let blah;
if (env === ‘production’) {
blah = config.production;
} else {
blah = config.development;
}

This is all pretty straightforward and simple to implement. However, the environment utilized by Node for either development or production is determined by a variable that is set on a global object. This is no problem if each environment exists on its own server. But what if you want to run a development and production environment on the same server using different project folders?

This presents a problem. I write 100% of my code on my iPad. And while my IDE of choice, play.js, offers the ability to run code in-app, there are tools that I only have available on my server via command line, such as linting and running tests. Which means that I’m not using my device as a local development environment; my development environment lives on my server. One solution would be to purchase an additional droplet to run strictly in development mode, but I’m not about to do that right now. I currently make $0 from writing code.

So what are my options, then, if I don’t want to keep updating the logic of my app every time I’m ready to merge to production? If part of the app setup contains logic to set the environment variable, this variable will be reset to development every time the test code is run. Not running a process like nodemon means I can start the app in production and it will continue to run in production mode until I restart the app, but I don’t want to depend on this sort of hack to bounce between environment types when I’m running tests and collecting logs. What happens if I forget to update the environment variable to production when I’m finished working in the development?

Calamity, that’s what. Extreme calamity.

Okay, exaggeration aside, at this moment I lack the fundamental knowledge of how Node operates to understand how to create a setup where I can effectively run development and production environments on the same server. Maybe it isn’t possible in Node’s current manifestation. There isn’t, however, anything clear on the matter, as far as I have looked. And parts of my search have taken me into the realms of workers and clusters and threads (oh my!).

I have much yet to learn, and I’m willing, I just feel like I’m a bit of a pioneer in this realm, traversing pathways that are traveled few and far between; and this, only because I have yet to find something definitive that tells me whether or not this is possible.

--

--

nonsensetwice
{nonsensecodes}

Reading & Writing. Music & Movement. Coffee & Code. Chaotic Great.