DON'T USE Environment Variables Without This

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025
  • This quick setup fixes environment variables in JavaScript.
    T3 env - env.t3.gg/docs...
    *Newsletter*
    Newsletter 🗞 - www.jamesqquic...
    *DISCORD*
    Join the Learn Build Teach Discord Server 💬 - / discord
    Follow me on Twitter 🐦 - / jamesqquick
    Check out the Podcast - compressed.fm/
    Courses - jamesqquick.co...
    *QUESTIONS ABOUT MY SETUP*
    Check out my Uses page for my VS Code setup, what recording equipment I use, etc. www.jamesqquic...

Комментарии •

  • @lucaszapico926
    @lucaszapico926 5 месяцев назад

    Thanks for the introduction to Zod very cool.

  • @LarsRyeJeppesen
    @LarsRyeJeppesen 11 месяцев назад +4

    Instead of this, I always make a central "config" object where I set all the environment variables. Also creates a type for this. Any place in my application that needs an environment varialbe will just import config and use the prop (environment variable) it needs. And because I typed config, I never miss a name

  • @gagiknavasatariyan7316
    @gagiknavasatariyan7316 11 месяцев назад +2

    This was really helpful for me, Thanks!

    • @JamesQQuick
      @JamesQQuick  11 месяцев назад

      So glad to hear that. Thanks for watching!!

  • @datra3237
    @datra3237 11 месяцев назад +1

    10:57 how would you go about handling the validation at build time?

    • @wobsoriano
      @wobsoriano 11 месяцев назад +1

      Ill do tsx env.ts && npm run build

  • @dechobarca
    @dechobarca 11 месяцев назад +23

    FYI you don't need to destructure process.env since zod will just drop any extra properties which aren't defined in your schema.

    • @sprioleau
      @sprioleau 11 месяцев назад +1

      +1. I think it’s best to keep the pre-defined properties (NODE_ENV, etc.) as part of the exported parsed environment object.

    • @dechobarca
      @dechobarca 11 месяцев назад +2

      ​@@sprioleau I think you possibly misunderstood what I was trying to say. The zod parsed object will NOT contain any properties that aren't defined in your schema. All that said, there is a schema method called passthrough that would preserve them without any validation.

    • @Goshified
      @Goshified 11 месяцев назад +1

      I don't think the destructuring has anything to do with validation -- the T3 Env docs for Next.js mention that the environment variables need to be destructrured because of how Next bundles environment variables. If the variable isn't defined explicitly, it'll get stripped out of the build.

  • @akvirus2
    @akvirus2 10 месяцев назад

    or you can use type augmentation to define your types. use zod for form validation or other stuff

  • @DmytroZhyvonitko
    @DmytroZhyvonitko 11 месяцев назад +11

    Let's install some packages and write a bunch of code to access and validate the types of our env variables that we control.

  • @sabuein
    @sabuein 6 дней назад

    Thank you.

  • @gosnooky
    @gosnooky 11 месяцев назад +1

    Environment variables at build time I think is only a modern thing with Next/Nuxt and these things. Normally environs are introduced at application start up time with the same ENV as the shell executing the application. Maybe certain ENV are not known at build time and depend on where the application is being deployed. I ran into this problem with Nuxt once and had to write a hack to get runtime environs into the application. It's a very strange thing to BUILD with environs.

  • @studio_egoryakimov
    @studio_egoryakimov 11 месяцев назад +1

    Really useful video, absolute like😃

  • @ayushshende4290
    @ayushshende4290 11 месяцев назад

    What about properties which are other that string like every field in an env file is string but in the schema let's say we have defined Port as a number, how would that work wouldn't that throw an error.

    • @xdneos
      @xdneos 11 месяцев назад

      You can use the property transform from zod

    • @xdneos
      @xdneos 11 месяцев назад

      Even you can use regex to be sure that the property only contain Numbers

  • @FreeCodeArena
    @FreeCodeArena 11 месяцев назад

    Nice video. I had to implement this recently in a project I'm working on. I usually couple it with a build script that validates the important env variables are set correctly before building the app.

  • @jajargg
    @jajargg 11 месяцев назад

    You can also just make global types like this:
    declare global {
    namespace NodeJS {
    interface ProcessEnv {
    DB_CONNECTION: string;
    DB_NAME: string;
    }
    }
    }

  • @kiikoh
    @kiikoh 11 месяцев назад +1

    This video has the right idea but this can be much simpler. Just export the results of .parse(). Every file you use needs to import process anyway so its no additional work

  • @technobanjo
    @technobanjo 11 месяцев назад

    lol I made a bun plugin for this yesterday and published on npm called bun-plugin-env-types. It gets all defined vars from all .env files them generates a env.d.ts file for you. so you can still use process.env, Bun.env or import.meta.env with autocomplete. Can use at build time or runtime.

  • @asteinerd
    @asteinerd 11 месяцев назад +3

    This seems like a HUGE amount of overhead...
    If there was a way to have it ONLY run when you're building/generating so it only does the checks during BUILD-TIME, and not run-time; that would be awesome.
    ... if it is... I'm missing it.
    [EDIT]: You said that right after I wrote this comment LOL

  • @gleitonfranco1260
    @gleitonfranco1260 11 месяцев назад +3

    I use IntelliJ btw ;-)

  • @CharcoalDaddyBBQ
    @CharcoalDaddyBBQ 11 месяцев назад

    But I set my environment variables.... I don't see the point of all this extra code...

    • @JamesQQuick
      @JamesQQuick  11 месяцев назад

      Do you not see value in intellisense and validation for those variables?

  • @jonathangamble
    @jonathangamble 11 месяцев назад

    use Valibot, way less overhead and has treeshaking

    • @Danielo515
      @Danielo515 11 месяцев назад

      Zod was a nice proof of concept, but nobody should be using it by now.

  • @IvanRandomDude
    @IvanRandomDude 11 месяцев назад +2

    We are seriously at the point where even using env variables is "hard"

  • @anasouardini
    @anasouardini 11 месяцев назад

    I was hoping it's a code-gen solution, not sure if ti's going to be good or bad, but I'm too lazy ¯\_(ツ)_/¯

  • @_neuromanser_
    @_neuromanser_ 11 месяцев назад

    Yes, add another dependency to project just to check env variable… 🙄

  • @LearningSpaceYC
    @LearningSpaceYC 4 месяца назад

    Overcomplicated. Should be much simpler from vendors for a such simple thing.

  • @Danielo515
    @Danielo515 11 месяцев назад +1

    This may be an impressive revelation... For those with 0 experience programming

    • @zapfska7390
      @zapfska7390 11 месяцев назад +2

      You earned a cookie for being so intelligent and knowledgeable

  • @gokulkrishh
    @gokulkrishh 11 месяцев назад +1

    Interesting james, so far i been using something like below.
    namespace NodeJS {
    interface ProcessEnv {
    [key: string]: string | undefined;
    NODE_ENV: 'development' | 'production';
    NEXT_PUBLIC_SUPABASE_URL: string;
    }
    }
    Though above mostly helped to typesafe env variable but not validation. I will try out Zod and see. Thanks for sharing.
    I know this setup is not super hard to do but would love to have CLI to automatically do this for me via ZOD by passing local env file in local and automate this inference and safety process in a file for me.

  • @immikegt
    @immikegt 5 месяцев назад +1

    And if just use ‘${process.env.EMAIL}’ ? The error disappeared and works.