In NestJS, I have something like this:
@Module({
imports: [
AnotherModule.register({
callBackUrl: 'http://localhost:3333/callback',
merchantId: '124312352134123',
currency: 'CAD',
}),
],
providers: [PaymentsService, ...PaymentsProviders],
exports: [PaymentsService],
})
export class PaymentsModule {}
Whenever I need to get merchantId from global variables or configuration module, NestJS has something called useFactory. but it works with registerAsync method, instead of register.
In this case, AnotherModule doesn't have registerAsync method. What can I do?
Question: why I can't use process.env here? (I know when I have configuration module, it's not the best solution but I want to know it's reason)
Thanks
Technically speaking, you have the access to the
process.env, but only to those variables that are already present in the environment, egNODE_PATH. If you're asking about variables from*.envfiles, and why they aren't accessible at this point, the answer is: because they haven't been loaded intoprocess.envby anything. When you use the NestJSConfigModule, it parses.envfiles, and loads their contents into environment, thusprocess.env.If you have a look at, say,
dotenvpackage (orpython-dotenv, in Python it works exactly alike), you need to load configuration from the files first, and only then, they become accessible to the application's environment.