Nestjs-easyconfig loads configs from your .env
(Wraps dotenv module) βοΈ π₯
$ npm install nestjs-easyconfig
$ yarn add nestjs-easyconfig
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({ path: './config/.env' })],
})
export class AppModule {}
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)
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
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
Module uses dotenv-parse-variables lib (https://www.npmjs.com/package/dotenv-parse-variables) to process env file. Example of type processing:
foo=test
bar=1
baz=true
qux=test,1,true,
bat=false*,
qwe=`1,2,3`,
asd=test,1,true*
will be processed to
{
foo: 'test',
bar: 1,
baz: true,
qux: ['test', 1, true],
bat: 'false',
qwe: '1,2,3',
asd: ['test', 1, 'true']
}
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/
andtest/
folders and/orREADME.md
. - Files in
dist/
folder are autogenerated when running tests (npm run build
) and need not to be changed manually.
- Most of your changes should be focused on
- Change or add tests if needed
- Run tests and make sure they pass
- Add changes to README.md if needed
- Make your changes and additions
- 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
- Author - Rubin Bhandari
- Devβ€to - @rubiin
- Discord - @rubin#1186
The package is MIT licensed.
Hey dude! Help me out for a couple of π»!
Thanks goes to these wonderful people (emoji key):
Dmitry Lukanin π |
Brad π π» |
Andras Bacsai π |
Eugen Istoc |
Jay McDoniel |
This project follows the all-contributors specification. Contributions of any kind welcome!