Get .env variable inside object in the controller NestJS

Viewed 262

I created .env

APP_PORT=3001
DEST=C:\data\formations

I have configService:ConfigService in the controller contractor. I want to get DEST for destination from .env in this code.

@UseInterceptors(FileInterceptor('file', {
  storage: diskStorage({
    destination: /*here*/ ,
    filename: (req, file, cb) => {
      const filename: string = path.parse(file.originalname).name.replace(/\s/g, '')
      const extension: string = path.parse(file.originalname).ext
      cb(null, `${filename}${extension}`)
    }
  })
}))

When I use this inside this other object I get an error.

2 Answers

If you want to use configuration outside Nest's IoC container, you can try nest-typed-config:

import { selectConfig } from 'nest-typed-config';
import { ConfigModule, FileConfig } from '@/module/config';

const fileConfig = selectConfig(ConfigModule, FileConfig);

@UseInterceptors(FileInterceptor('file', {
  storage: diskStorage({
    destination: fileConfig.destination,
    filename: (req, file, cb) => {
      const filename: string = path.parse(file.originalname).name.replace(/\s/g, '')
      const extension: string = path.parse(file.originalname).ext
      cb(null, `${filename}${extension}`)
    }
  })
}))
Related