简体   繁体   中英

Can't pull sqs queue name from .env file in nestjs

When pulling the queue name through.env file I get the following error on the console: Cannot read properties of undefined (reading 'meta').

How can I pass queue name from.env file to nestjs @SqsMessageHandler decorator?. I am using @ssut/nestjs-sqs library.

 @SqsMessageHandler(process.env.QUEUE_NAME, false)
       public async handleMessage(message: AWS.SQS.Message) {
        console.log(message);
       }
    
       @SqsConsumerEventHandler(process.env.QUEUE_NAME,'processing_error',
       )
       public onProcessingError(error: Error, message: AWS.SQS.Message) {
        // report errors here
        console.error(error);
        console.error(message);
    }

The problem is that the process.env.QUEUE_NAME is not available at the time you're using it. You'll need invoke dotenv (or whatever package that read & parse the env file) by yourself if you want to use process.env. like this.

imports: [
    ...
    ConfigModule.forRoot({
      envFilePath: '.env',
    }),
  ],

You can add env file import like this in your Sqs module, which is >the standard way of using.env files in a NestJS module.

I realized later that this doesn't work for registering the queue, or for the decorators. We ran into the same issue, the env vars are available inside the modules but not during build, and nest ignores

Depending on how you start your services you do these:

NPM Scripts

Inside your package.json add the env vars:

"start:dev": "QUEUE_NAME=your_queue_name nest start --watch"

Docker

Add your env var before the node build step inside your Dockerfile .

ENV QUEUE_NAME your_queue_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM