NestJS Config Module: Using environment variables

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025
  • Nestjs Full Course: Episode 12
    This video is about accessing environment variables in NestJS applications.
    I start by introducing the problem of accessing environment variables in NestJS and explain that it can be done using the config module. I then demonstrate how to install and configure the config module in a NestJS project.
    I also discusses some best practices for organizing configurations, such as creating custom configuration objects and managing different environment files. Finally, I provide a recap of the steps involved in accessing environment variables in NestJS applications.
    Overall, this video provides a comprehensive overview of how to access and manage environment variables in NestJS applications. It covers both basic and advanced topics, making it suitable for developers of all levels.
    GitHub Repo(Please Give It a Star⭐ on GitHub): github.com/vah...

ความคิดเห็น • 30

  • @AhmedMansour-tg5ez
    @AhmedMansour-tg5ez 19 วันที่ผ่านมา

    A dense, yet fruitful video
    didn't know .env and config can do all fo that
    Thank You Vahid

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      Thanks for watching, I'm glad you found it helpful!

  • @Mahadev-x7u
    @Mahadev-x7u 6 หลายเดือนก่อน +1

    The Best course of Nest js 💯 thank you 🔥

    • @SakuraDev
      @SakuraDev  6 หลายเดือนก่อน +1

      Thank you so much for your support ❤️🙏

  • @mohammadbohluli
    @mohammadbohluli 4 หลายเดือนก่อน

    very nice bro, thnaks(دمت گرم)❤❤❤❤❤❤

    • @SakuraDev
      @SakuraDev  4 หลายเดือนก่อน

      Mamnoon ❤️

  • @dreamsachiever212
    @dreamsachiever212 6 หลายเดือนก่อน

    great one

    • @SakuraDev
      @SakuraDev  6 หลายเดือนก่อน +1

      My pleasure! 😊

  • @rheumaticharm9551
    @rheumaticharm9551 3 หลายเดือนก่อน

    Nice Video!!

    • @SakuraDev
      @SakuraDev  3 หลายเดือนก่อน

      Thank you! I’m glad you liked it. More videos are coming soon, so stay tuned! 👍😊

  • @eminental_
    @eminental_ หลายเดือนก่อน

    which theme are you using?

    • @SakuraDev
      @SakuraDev  หลายเดือนก่อน

      It's bearded theme

  • @venobrun
    @venobrun 3 หลายเดือนก่อน

    after adding env variables. our seed functionality is broken

    • @SakuraDev
      @SakuraDev  3 หลายเดือนก่อน

      Hi, what is the error?

  • @belkocik
    @belkocik 6 หลายเดือนก่อน

    What about type safe env (would love to see autocompletion intellisense to my env)

    • @SakuraDev
      @SakuraDev  6 หลายเดือนก่อน

      It can easily be done with joi package. I will create an extra episode for this. But at end of course. For now we just turn out focus on authentication

    • @robertz7329
      @robertz7329 5 หลายเดือนก่อน

      @@SakuraDev can it be done with Zod?

    • @SakuraDev
      @SakuraDev  5 หลายเดือนก่อน

      @@robertz7329 I should test it. It would be great if we could do it with zod

  • @aawave
    @aawave 22 วันที่ผ่านมา

    I come from another language/framework. Some solutions you apply seem strange to me:
    1. It seems hard to believe that such a basic feature as .env file support requires so much setup in NestJS. Is this the general way to do it, or is this applicable for your specific use case in your specific project? What is the basis that is generally enough for production level NestJS applications?
    2. Why don't you use ConfigService? Your approach without ConfigService leads to manual type conversion, which does not seem to be a good practice.
    3. Why would you disable TypeORM synchronize in prod, but enable it in dev? Won't this hide incorrect migrations and lead to bugs in stage/prod?

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      Thank you for your detailed questions! 😊 Let me address each point:

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      1- .env Setup in NestJS:
      Great observation! While .env support can feel more straightforward in some frameworks, NestJS intentionally separates concerns to align with its modular architecture. Using the @nestjs/config package for .env management is considered the standard way in NestJS, providing flexibility and scalability for larger applications. While it may seem like more setup initially, this approach ensures you can easily manage configurations across modules, which is particularly helpful for production-grade applications.

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      2- ConfigService Usage:
      You’re absolutely right about the benefits of ConfigService-it abstracts away manual parsing and type conversion, making configuration handling cleaner and more robust. In this project, I chose a more direct approach to show how the system works at a lower level, but I agree that using ConfigService is a better practice for production. I'll make a note to cover this in future videos to highlight the advantages!

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      3- TypeORM Synchronize:
      Enabling synchronize in development is helpful because it simplifies schema updates during active development. However, in production, it’s best to disable this and rely on proper migrations to ensure controlled and predictable database changes. You're correct that failing to test migrations thoroughly in a staging environment can lead to bugs in production. Best practice is to rigorously test migrations in a staging environment before deployment.

    • @SakuraDev
      @SakuraDev  19 วันที่ผ่านมา

      By the way, if you enjoy learning and want to dive deeper into computer science topics made simple, I run another channel where I summarize key concepts from CS books. I would be very glad if you subscribe to my new channel: th-cam.com/channels/ZYhXrFwhFUOXuSQNgfk-Sw.html

  • @FejkMarcin1212
    @FejkMarcin1212 5 หลายเดือนก่อน

    Why this.configService.get("dbconfig.env.type) is not type safe and there is not autocompletion intellisense? It is just a string.

    • @SakuraDev
      @SakuraDev  5 หลายเดือนก่อน +1

      Well, the config service does not have type safety out of the box. We need to use third party packages to add type safety

    • @FejkMarcin1212
      @FejkMarcin1212 5 หลายเดือนก่อน

      ​@@SakuraDev I will keep this in mind. Thank you :)

    • @SakuraDev
      @SakuraDev  5 หลายเดือนก่อน

      @@FejkMarcin1212 🙏🙏👋