NestJS: Work with configuration
Tags: javascript, node, nestjs, beginners
Published: 2022-08-10 07:38:00

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 <u>.env</u> files.**
We can store configuration data in .js or .ts files. And exclude these files via <u>.gitignore</u> from the repository. But usually environment variables are used for such cases. For this, <u>.env</u> 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
```

And in order to protect the data from getting into the repository, we will write exceptions in <u>.gitignore</u>, and also create a file with name <u>.env.example</u> . 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=
```
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 <u>.env</u> 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 <u>.env</u> files. Lets take a look how we can use env variables in NestJS app.

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

```
npm i @nestjs/config
```

After that, in the Module's import field add <u>ConfigModule.forRoot()</u> 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],
})
```

In the <u>controller</u> constructor:

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

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

  // Other code....
}
```

In the same way, you can use <u>configService</u> in other parts of the application.

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