-
-
Save emafriedrich/5b4cbc53f5691bed4b48ddc60a639f43 to your computer and use it in GitHub Desktop.
env node module. Typescript implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MY_ENV_VAR=some-value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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