My favorite way to manage config file (javascript example)

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • create-react-app

    Set up a modern web app by running one command.

  • Now I will show you my code. But first I want to tell you that I got this inspiration from create-react-app source code.

  • dotenv

    Loads environment variables from .env for nodejs projects.

  • You need 2 libraries which are dotenv and dotenv-expand.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • dotenv

    A Ruby gem to load environment variables from `.env`. (by bkeepers)

  • // environments.js // Inspiration from https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/env.js const fs = require('fs') const path = require('path') const dotenv = require('dotenv') const dotenvExpand = require('dotenv-expand') if (!process.env.NODE_ENV) { process.env.NODE_ENV = 'development' } function getEnvPath() { return path.resolve(__dirname, '../', '.env') } function getNodeEnv() { return process.env.NODE_ENV.trim() } // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use const dotenvFiles = [ `${getEnvPath()}.${getNodeEnv()}.local`, // Don't include `.env.local` for `test` environment // since normally you expect tests to produce the same // results for everyone getNodeEnv() !== 'test' && `${getEnvPath()}.local`, `${getEnvPath()}.${getNodeEnv()}`, getEnvPath(), ].filter(Boolean) // Load environment variables from .env* files. Suppress warnings using silent // if this file is missing. dotenv will never modify any environment variables // that have already been set. Variable expansion is supported in .env files. // https://github.com/motdotla/dotenv // https://github.com/motdotla/dotenv-expand dotenvFiles.forEach(dotenvFile => { if (fs.existsSync(dotenvFile)) { dotenvExpand( dotenv.config({ path: dotenvFile, }) ) } })

  • dotenv-expand

    Variable expansion for dotenv. Expand variables already on your machine for use in your .env file.

  • // environments.js // Inspiration from https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/env.js const fs = require('fs') const path = require('path') const dotenv = require('dotenv') const dotenvExpand = require('dotenv-expand') if (!process.env.NODE_ENV) { process.env.NODE_ENV = 'development' } function getEnvPath() { return path.resolve(__dirname, '../', '.env') } function getNodeEnv() { return process.env.NODE_ENV.trim() } // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use const dotenvFiles = [ `${getEnvPath()}.${getNodeEnv()}.local`, // Don't include `.env.local` for `test` environment // since normally you expect tests to produce the same // results for everyone getNodeEnv() !== 'test' && `${getEnvPath()}.local`, `${getEnvPath()}.${getNodeEnv()}`, getEnvPath(), ].filter(Boolean) // Load environment variables from .env* files. Suppress warnings using silent // if this file is missing. dotenv will never modify any environment variables // that have already been set. Variable expansion is supported in .env files. // https://github.com/motdotla/dotenv // https://github.com/motdotla/dotenv-expand dotenvFiles.forEach(dotenvFile => { if (fs.existsSync(dotenvFile)) { dotenvExpand( dotenv.config({ path: dotenvFile, }) ) } })

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts