Skip to content

Instantly share code, notes, and snippets.

@arn4v
Created February 11, 2021 06:55
Show Gist options
  • Save arn4v/656938b86c8dc26339d7dde8cb4ef446 to your computer and use it in GitHub Desktop.
Save arn4v/656938b86c8dc26339d7dde8cb4ef446 to your computer and use it in GitHub Desktop.
Simple dotenv for NodeJS
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