Reading Environment Variables In NodeJS REST API

Learn 5 ways with an example project

Bhargav Bachina
Bachina Labs

--

Photo by Siora Photography on Unsplash

Reading environment variables is one of the most common things that we do when we are building apps. It doesn’t matter whether you are developing front end app or backend API you have so many variables that should be outside of your application source code that makes your app or API more configurable. For example, if you want to hide logger statements in production or do something else based on the environment you can pass this as an environment variable. If you want to change later all you need to change in one place.

In this post, we will see multiple ways to do this we can use webpack DefinePlugin, dotenv, or just pass arguments when running node.

  • Example Project
  • Passing as arguments
  • With dotenv library
  • Using dotenv for Different Environments
  • With Webpack DefinePlugin
  • Using DefinePlugin for Different Environments
  • With Webpack EnvironmentPlugin
  • With DotenvPlugin Plugin
  • Summary
  • Conclusion

Example Project

--

--