webenv 0.5.3
Install from the command line:
Learn more about npm packages
$ npm install @socheatsok78/webenv@0.5.3
Install via package.json:
"@socheatsok78/webenv": "0.5.3"
About this version
Loads variables from .env
for web projects.
What is Dotenv?
Dotenv is a zero-dependency module that loads environment variables from a .env
file into process.env
. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
Why Webenv?
While dotenv
provide a convenient way of loading environment variables from a .env
file for Node.js, however managing environment variables for the web platform doesn't seem to be any easier.
This project is aimed to fix the those on the web platform by using fetch
to request the .env
file from your publicly accessible path.
It a highly customized dotenv
and dotenv-expand
functionality.
npm install @socheatsok78/webenv --save
Or installing with yarn?
yarn add @socheatsok78/webenv
Create a .env
file in the public folder of your project:
API_BACKEND_ENDPOINT="https://api.example.com"
As early as possible in your application, import and configure dotenv:
import * as webenv from '@socheatsok78/webenv'
(async () => {
await webenv.config({ path: '/.env' }) // make a fetch request to '/.env' and parse the string response
console.log(window.env) // remove this after you've confirmed it working
})()