Created
February 11, 2021 06:55
-
-
Save arn4v/656938b86c8dc26339d7dde8cb4ef446 to your computer and use it in GitHub Desktop.
Simple dotenv for NodeJS
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
const path = require("path") | |
const ROOT = process.cwd() | |
const ENV_PATH = path.resolve(path.join(ROOT, ".env")) | |
module.exports.dotenv = function dotenv() { | |
fs.readFileSync(ENV_PATH) | |
.toString() | |
.split("\n") | |
.filter((i) => i.length > 0) | |
.forEach((i) => { | |
i = i.replace(/\=/, "__").split("__") | |
i[1] = i[1].replace(/(?:\\[rn]|[\r\n]+)+/g, "") | |
if (i[1][0] === '"') { | |
i[1] = i[1].replace(/"/g, "") | |
} | |
process.env[i[0]] = i[1] | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment