
NestPack Config
A reusable Configuration Module for NestJs.
Features:
- .env file support
- programmatic overrides and defaults
- process.env support
- constants autogeneration
Installation
$ npm install @nestpack/config
$ yarn add @nestpack/config
Basic Usage
Try it out on codesandbox (You may need to restart the sandbox on changes.)
# development.env
TEST_VAR= It worked!!
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ConfigModule } from '@nestpack/config';
@Module({
imports: [
ConfigModule.register(),
],
controllers: [AppController],
})
export class AppModule {}
import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestpack/config';
@Controller()
export class AppController {
constructor(private readonly configService: ConfigService) {}
@Get()
getHello(): string {
this.configService.get('TEST_VAR');
}
}
How it Works
On application start, the ConfigModule
will load either [environment].env
or development.env
(as a fallback). If no .env
file exists, then none will be loaded.
When accessing variables through ConfigService.get()
, the service will attempt to load variables in the order of:
overrides
from options
process.env
.env
file variables
defaults
from options
Overrides and Defaults
In the ConfigModule
options object, overrides
and defaults
can be provided to include variables programatically.
@Module({
imports: [
ConfigModule.register({
defaults: {
FROM_DEV_ENV: 'default will be used if not defined anywher else',
WILL_BE_OVERRIDDEN: 'Value will not be used',
},
overrides: {
WILL_BE_OVERRIDDEN: 'Value used',
},
}),
],
controllers: [AppController],
})
export class AppModule {}
Constants generation
Since Nest is developed in a Typescript context, it's useful to include constants for accessing string based values by name. The ConfigModule
can optionally generate a constants file to be used in the project. Constants are generated by combining the .env
file, overrides
, and defaults
when the application loads. If a new environmental option has bene set somewhere, simply reload the application to regenerate the file. Note that a file will not be generated if the options haven't changed, as to not trigger a tsc --watch
reload.
Constants Usage
@Module({
imports: [
ConfigModule.register({
generateConstants: true,
constantsOutputDir: path.resolve(__dirname, './cosntants/'),
generateConstantsEnviroments: ['development', 'test'],
defaults: {
VAR_ONE: '1',
},
overrides: {
VAR_TWO: '2',
},
}),
],
controllers: [AppController],
})
export class AppModule {}
# development.env
VAR_THREE=3
When the application loads, it will generate a constants file like this:
export const CONFIG = {
VAR_ONE: 'VAR_ONE',
VAR_TWO: 'VAR_TWO',
VAR_THREE: 'VAR_THREE',
};
With the constants file in place, the keys can be accessed through a reference:
import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestpack/config';
import { CONFIG } from './config.constants';
@Controller()
export class AppController {
constructor(private readonly configService: ConfigService) {}
@Get()
getHello(): string {
return this.configService.get(CONFIG.VAR_ONE);
}
}
Common Problems
NPM/Yarn Link
When using this package with NPM link, the projectRoot
option will need to be set as the package cannot automatically determine the location under that scenario.
process.env constants
The constants generation does not look at process.env
because there are too many variables to take into account. If an environmental variable will only ever be set by the process environment, consider adding an empty default.
License
MIT licensed.