Nestjs, I can't access my environment variables from my static function of the module

719 Views Asked by At

I want to create a dynamic module with Nest.js, this module aims to provide a variable, initialized in this function, in the rest of my application.

This variable must be initialized according to my environment variables, which are stored in an .env file of my project.

When I try to access it from a service there is no problem (with process.env and ConfigModule injection).

But when I want to do it from my static function forRoot, the environment variables in question is null.

.env file enter image description here

AppModule: enter image description here

CoreModule: enter image description here

FirebaseModule: enter image description here

In the FirebaseModule, my environment variables are null.

What I tried:

  • I tried to move the ConfigModule import to the CoreModule instead of the AppModule, and in this case I can access my environment variables
  • I tried to add the required configuration in the arguments of the forRoot function:
@Module({
    imports: [
        ConfigModule,
        FirebaseModule.forRoot({
            url: process.env.FIREBASE_ADMIN_DATABASE_URL,
            key: process.env.FIREBASE_ADMIN_PRIVATE_KEY,
            id: process.env.FIREBASE_ADMIN_PROJECT_ID,
            email: process.env.FIREBASE_ADMIN_CLIENT_EMAIL
        })
    ]
})
export class CoreModule {
}

But this does not solve my problem.

Does anyone know how to solve this problem? Thanks

0

There are 0 best solutions below