Description
Nestjs-easyconfig loads configs from your .env
(Wraps dotenv module) ⚙️ 🔥
Installation
$ npm install nestjs-easyconfig
$ yarn add nestjs-easyconfig
Usage
With config file supplied (basic):
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({ path: './config/.env' })],
})
export class AppModule {}
With config file supplied and safe set to true:
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({ path: './config/.env', safe: true })],
})
export class AppModule {}
By default safe is set to false. When safe is set to true
, the module compares the supplied env
file with the sample env file to find missing keys. If any keys which are in .env.sample but not in the evironment used, it is immediately reported in console.
Note
: To use this, a sample env file .env.sample
should be placed in the root dir
Other config include dotenv's configs like encoding (Default: utf8) and debug(Default: false)
Without config file supplied:
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({})],
})
export class AppModule {}
In this case, you have to pass in the NODE_ENV value and the .env
file to read will be determined accordingly.
Loads environment variables from .env.[development|test|production][.local]
files
For example, NODE_ENV=dev will make the app read .env.dev
Note: The .env file also has to be in root folder
Getting environment variables
Regardless of how the EasyconfigModule
is imported into the app, you can get the variable values using the EasyconfigService
.
import { Controller, Get } from '@nestjs/common';
import { EasyconfigService } from 'nestjs-easyconfig';
@Controller('api')
export class AppController {
constructor (private config: EasyconfigService) {}
@Get()
findAll() {
return {
value: this.config.get('key')
};
}
}
Note: the get
method will automatically cast environment variables
Type processing
Example of type processing:
Imagine you have a configuration file at .env
with the following:
FOO=bar
BAZ=2
BEEP=false
BOOP=some,thing,that,goes,wow
BLEEP=false*
PING=ping,true*,2,100
PONG=`some,thing,that,goes,wow`
After using this plugin, the environment variables are parsed to their proper types.
To test it out, simply log the returned object in your console:
console.log(env);
And you'll see that it outputs the properly parsed variable types:
{
FOO: 'bar',
BAZ: 2,
BEEP: false,
BOOP: [ 'some', 'thing', 'that', 'goes', 'wow' ],
BLEEP: 'false',
PING: [ 'ping', 'true', 2, 100 ],
PONG: 'some,thing,that,goes,wow'
}
If your configuration line ends in *
it will not be parsed by this package, which allows you to keep values as the String
variable type if needed. Also when you encapsulate a value between bacticks e.g. `value`, the value won't be parsed and it will return as a String
variable. This can be used in situations where you for example have a ',' inside your string and it should not be parsed as an array.
Config
path ? : string;
sampleFilePath ? : string;
safe ? : boolean;
debug ? : boolean;
parseLog ? : boolean;
encoding ? : string;
logger ? : LoggerService;
assignToProcessEnv ? : boolean;
overrideProcessEnv ? : boolean;
Contributing
In general, we follow the "fork-and-pull" Git workflow.
- Fork the repo on GitHub
- Clone the project to your own machine
- Work on your fork
- Make your changes and additions
- Most of your changes should be focused on
src/
and test/
folders and/or README.md
. - Files in
dist/
folder are autogenerated when running tests (npm run build
) and need not to be changed manually.
- Change or add tests if needed
- Run tests and make sure they pass
- Add changes to README.md if needed
- Commit changes to your own branch
- Make sure you merge the latest from "upstream" and resolve conflicts if there is any
- Repeat step 3(3) above
- git add and run npm run commit and fill in the details accordingly
- Push your work back up to your fork
- Submit a Pull request so that we can review your changes
Stay in touch
License
The package is MIT licensed.
Support on Beerpay
Hey dude! Help me out for a couple of :beers:!
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!