
Member-only story
process.env: What it is and why/when/how to use it effectively
So you’ve gotten past your first few tutorials in Node.js and you’ve probably seen the line app.listen(process.env.PORT)
or something to that effect. Why not just specify the port as 3000
instead of typing sixteen characters?
What it is
The process.env
global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH
variable set, this will be made accessible to you through process.env.PATH
which you can use to check where binaries are located and make external calls to them if required.
Why environment is important
An application needs to be deployed in order for it to be useful, and this can be anything from code that creates a simple website to complex APIs that engage in intensive computations. Ultimately, if an application is not deployed, no one can use it and it serves no purpose.
When we write our code, we can never be sure where our application can be deployed. If we require a database in development, we spin up an instance of it and we link to it via a connection string — something like 127.0.0.1:3306
. However, when we deploy it to a server in production, we…