DEV Community

Ilya R
Ilya R

Posted on

NestJS: Work with configuration

Application configuration is often understood as setting a particular state of application. The simplest example is running an application in development or production mode. For example, in development mode, we need the application to show us the maximum information about errors. But when we run the application in production, it is not the best idea to show such information to ordinary users.

Also, the configuration means set up of credentials for working with the database and third-party services.

We can keep such data in different files in different places of the application, but it will be hard to maintain in future. For example, when the login or password from the database are changed, we would need to change all that stuff in all files.

Therefore, it is better to keep all configurations in one file or in one directory. But, you should also remember about security and avoid getting these files into the repo. Even if the repository is private.

Using .env files.
We can store configuration data in .js or .ts files. And exclude these files via .gitignore from the repository. But usually environment variables are used for such cases. For this, .env file is created in the root of the project and all the configuration data is written there in the "key = value" format.

It will look something like this:

PORT=3000
MONGO_ROOT_NAME=root
MONGO_ROOT_PWD=example
APP_NAME=Supershop
MONGO_URL=mongodb://user1:example@mongo:27017/dbname
SALT=43fdf6132sda8b94846d1a531d6fdffd
Enter fullscreen mode Exit fullscreen mode

And in order to protect the data from getting into the repository, we will write exceptions in .gitignore, and also create a file with name .env.example . It will be stored in the repository. Such a file will contain the required fields, but without values. For example:

PORT=
MONGO_ROOT_NAME=
MONGO_ROOT_PWD=
APP_NAME=
MONGO_URL=
SALT=
Enter fullscreen mode Exit fullscreen mode

This file need to understand which env variables are used in application. After you clone project from repo, you will need just rename file to .env and fill all fields.

Next, we can use anywhere in our application the environment variables taken from this file.

Use configuration in NestJS.
NestJS has its own ways of working with .env files. Lets take a look how we can use env variables in NestJS app.

For this we need to install @nestjs/config in our application.

npm i @nestjs/config
Enter fullscreen mode Exit fullscreen mode

After that, in the Module's import field add ConfigModule.forRoot() so that access to environment variables is available globally.

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

// Other code

@Module({
  imports: [
    ConfigModule.forRoot(),
    // Another imports
  ],
  controllers: [AppController],
  providers: [AppService],
})
Enter fullscreen mode Exit fullscreen mode

In the controller constructor:

import { ConfigService } from '@nestjs/config';

@Controller()
export class AppController {
  constructor(private readonly configService: ConfigService) {}

  // Other code....
}
Enter fullscreen mode Exit fullscreen mode

In the same way, you can use configService in other parts of the application.

This is how basic configuration work looks like in a NestJS application.

Top comments (1)

Collapse
 
galkin profile image
Nikita Galkin

Dependency Injection for application configuration makes sense only if you mock the dependency in unit testing. most applications do not use dynamic configuration initialization (reading from consul, secret manager, etc). Because of that i prefer to config.ts file for reading env vars and import it without dependency injection. This approach doesn't create over engineering. The same about logger.