Skip to content

Instantly share code, notes, and snippets.

@jerolan
Last active August 21, 2019 15:38
Show Gist options
  • Save jerolan/a8531134279dd279a823c7748a75e0b7 to your computer and use it in GitHub Desktop.
Save jerolan/a8531134279dd279a823c7748a75e0b7 to your computer and use it in GitHub Desktop.
const customEnv = require('custom-env');
const NODE_ENV = process.env.NODE_ENV || 'development';
/**
* Shares behaviour with CRA for envs
* https://create-react-app.dev/docs/adding-custom-environment-variables#what-other-env-files-can-be-used
**/
customEnv.env();
customEnv.env('local');
customEnv.env(NODE_ENV);
customEnv.env(`${NODE_ENV}.local`);
const { MY_ENV_VAR } = process.env;
const defaults = { MY_ENV_VAR: null };
// Alert to fill the necessary environment variables
Object.keys(defaults).forEach(key => {
if (!process.env[key]) {
throw new Error(
`Please enter a custom ${key} in .env.${NODE_ENV} on the root directory`
);
}
});
module.exports = {
NODE_ENV,
MY_ENV_VAR,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment