Skip to main content

Configurations

Environment Variables

Before start using your project, you need to configure few variables first. The variables are self explanatory.

.env
APP_NAME=
APP_ENV=
APP_DEBUG=
APP_URL=
APP_PORT=

DB_TYPE=mysql2 #db driver name
DB_HOST=localhost
DB_PORT=3305
DB_USER=root
DB_PASSWORD=root
DB_DATABASE=nestjs_test
DB_DEBUG=0

Configuration

NestJS provides a very elegant module for integrating configuration in your application.

This boilerplate, uses the Config Module provided by NestJS but in an opinionated way, which best suits your workflow.

In the config/ directory, we have added few configuration files, which preload many configuration for you.

We recommend using only namespaced configurations only.

Examples

config/app.ts
import { registerAs } from '@nestjs/config';

export default registerAs('app', () => ({
name: process.env.APP_NAME || 'NestJS App',
env: process.env.APP_ENV || 'local',
debug: +process.env.APP_DEBUG || 1,
url: process.env.APP_URL || 'localhost',
port: +process.env.APP_PORT || 5000,
}));
config/database.ts
import { registerAs } from '@nestjs/config';
import { basePath } from '@app/core';

export default registerAs('db', () => ({
type: process.env.DB_TYPE || 'mysql2',
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
username: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.DB_DATABASE || 'test',
}));
config/services.ts
import { registerAs } from '@nestjs/config';

// all third-party services' configurations to go here
export default registerAs('services', () => ({}));
config/settings.ts
import { registerAs } from '@nestjs/config';

// all your application settings go here.
export default registerAs('settings', () => ({}));