Skip to content

Instantly share code, notes, and snippets.

@emafriedrich
Last active August 25, 2020 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emafriedrich/5b4cbc53f5691bed4b48ddc60a639f43 to your computer and use it in GitHub Desktop.
Save emafriedrich/5b4cbc53f5691bed4b48ddc60a639f43 to your computer and use it in GitHub Desktop.
env node module. Typescript implementation
MY_ENV_VAR=some-value
import * as fs from 'fs';
import * as path from 'path';
// depends on your current execution path. If you script are on same path of .env file, this works.
const envFilePath = path.resolve() + '.env';
export function config() {
if (fs.existsSync(envFilePath)) {
const fileContent = fs.readFileSync(envFilePath, 'utf8');
const arrEnv = fileContent.split('\n');
const parsed = arrEnv.map((envVar) => envVar.split('='))
for (const env of parsed) {
process.env[env[0]] = env[1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment