Envfile
Synchronous provider that sources variables from envfiles. In order to use it you need to create a helper to tell which envfiles to use.
But first... installation
- npm
- Yarn
- pnpm
npm install @pallad/config-envfile
yarn add @pallad/config-envfile
pnpm add @pallad/config-envfile
Creating envfile helper.
import {envFileProviderFactory} from '@pallad/config-envfile';
const envFile = envFileProviderFactory({
files: ['.env'] // loads ".env" from current working directory
})
envFile('FOO') // uses `FOO` variable from ".env" file
// converts to int
envFile('FOO').transform(type.int);
// converts it to int, if not available uses: 1000
envFile('FOO').transform(type.int).defaultTo(1000);
Customization
import {envFileProviderFactory} from '@pallad/config-envfile';
const envFile = envFileProviderFactory({
files: [
// ignores if does not exist
{path: 'development.env', required: false},
// additionally loads production envfile and overrides all variables with same name defined in previous files
{path: 'production.env', required: false},
'.env' // requires file to exist
],
cwd: '../config', // custom working directory - default process.cwd
});
Populating to ENV
Variables loaded from ENV files are not applied to process.env
by default.
You can however populate them by calling populateToEnv
const envFile = envFileProviderFactory({
files: [
// ignores if does not exist
{path: 'development.env', required: false},
// additionally loads production envfile and overrides all variables with same name defined in previous files
{path: 'production.env', required: false},
'.env' // requires file to exist
],
cwd: '../config', // custom working directory - default process.cwd
});
envFile.populateToEnv(); // populates all loaded variables to process.env
// populates only the ones that satisfy the predicate
envFile.populateToEnv((key) => {
return key === 'AWS_REGION';
})