Our great sponsors
-
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.
-
dotenv-expand
Variable expansion for dotenv. Expand variables already on your machine for use in your .env file.
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.
You need 2 libraries which are dotenv and dotenv-expand.
// 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, }) ) } })
// 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, }) ) } })