#Passing config values to @RabbitSubscribe decorator
41 messages · Page 1 of 1 (latest)
It's not possible to use the ConfigService inside a decorator. You can use dotenv to populate process.env and use process.env inside the decorator instead
When the decorator is initialized process.env is empty, only after that the configurations are loaded into it 😦
That'swhy I said to use dotenv to populate process.env. If you do that as the first lines in your main.ts then process.env will be properly populated
You mean not the ConfigModule but pure dotenv?
And I mean
import { config } from 'dotenv'
config()
As lines 1 and 2
This seems kinda hacky
Why?
What if I wanted to load values from Azure for example?
Using the @azure/app-configuration package
Well in that case I suppose I could load the URL from config to environment variables then use that with the package in the decorator
Overall, what you're seeing is an issue with how decorators are processed at the time of file import. It might be possible to use a lazy loader function and reference the class's properties to get the ConfigService, but that would certanily be hacky
Yeah I tried stuff like that, using a static property on a class for example...
The only thing other than what you suggested that allows you to "outsource" a value and have the decorator be familier with it is defining a variable outside of any scope but then it's hardcoded and I didn't solve anything 😛
Anyway, thanks for the answer, I'll try to do what you suggested
Update: that didn't work either, the decorator is initialized even before the bootstrap sequence...
That's not true. Dedcorators are higher order functions that are ran one file import. What does your main.ts look like?
async function bootstrap(): Promise<void> {
config({ path: `../${process.env.NODE_ENV ?? 'na'}.env` });
console.log(process.env.TEST);
That's not lines 1 and 2 is it?
It is
That's the first lines of the bootstrap, not of the file
Ohhhh
They need to be the very first lines of the file
I see, it's the import that makes this happen
Literal, lines 1 and 2
By importing I make it load the decorator
Well, I just moved those lines to the very first and second lines of the main.ts files and I have debug on and it still seems like the decorator's breakpoint is hit before the dotenv config breakpoint...
Yeah, nothing I tried works...
Hmm, that should be fine. Got a reproduction?
I just left the office at work so I'll work on that later/tomorrow
I tried creating the same scenario with a new project and it works there...
Could it be because I set the project with the problem to ES2022?
That shouldn't effect it
Weird.. In the project I just created, in debug mode I can see that it reaches the code line first then it reaches the decorator
And in my project it's the other way around
And the only difference I can see is one is CommonJS and the other is ES2022
I don't believe ES2022 will change that behavior, but you can give it a shot
Well, yep, that's the reason
I can send you a link to the repo if you wish
I changed in tsconfig "module" and "target" to ES2022, added moduleResolution: "node" and changed in package.json to "type": "module" and that makes the problem happen
I think it's actually loading the file which has the @RabbitSusbcribe decorator first, not necessarily the decorator itself, since when I put the import { config } from 'dotenv' statement there it does work
Most be how es2022 loads packages and files. I wonder if ts is hoisting something during compilation. I can take a look if you've got the reproduction