Load configuration by environment variable in NestJS application

In nestJS application, you can load the config variable with a corresponding environment variable by you can following these steps:
1. install nestjs-config package from the nestjs-config:

2. create folder structure with environments folder and config folder

Environment file with structure .env[environment] (for example development, production…), you can add another environment, such as: qc, staging… (.env.qc or .env.staging) and edit package.json file following:

3. In app.module.ts file, you can load the config module with the code bellow

With the configuration above, then you can get config from nestJS application anywhere you want by injecting ConfigService service, for example: get pageSize config from the environment with ConfigService

The ConfigService is injected through the class constructor or you can use property-based injection

You can click the link below to get more information from the nestjs-config package

You May Also Like

About the Author: truongluu

Leave a Reply

Your email address will not be published. Required fields are marked *