DON'T USE Environment Variables Without This

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

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

  • @dechobarca
    @dechobarca 3 месяца назад +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 3 месяца назад +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 3 месяца назад +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 3 месяца назад +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.

  • @LarsRyeJeppesen
    @LarsRyeJeppesen 3 месяца назад +3

    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

  • @DmytroZhyvonitko
    @DmytroZhyvonitko 3 месяца назад +10

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

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

    This was really helpful for me, Thanks!

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

      So glad to hear that. Thanks for watching!!

  • @meowwyay
    @meowwyay 3 месяца назад +1

    Really useful video, absolute like😃

  • @Buy_YouTube_Views_a135
    @Buy_YouTube_Views_a135 3 месяца назад

    Love this improvement

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

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

  • @davidtran3237
    @davidtran3237 3 месяца назад +1

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

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

      Ill do tsx env.ts && npm run build

  • @gosnooky
    @gosnooky 3 месяца назад +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.

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

    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.

  • @kiikoh
    @kiikoh 3 месяца назад +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

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

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

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

    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 3 месяца назад

      You can use the property transform from zod

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

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

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

    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 3 месяца назад +3

    I use IntelliJ btw ;-)

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

    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.

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

    use Valibot, way less overhead and has treeshaking

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

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

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

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

    • @YouTubePL666
      @YouTubePL666 3 месяца назад +1

      Just RUclipsrs making this “hard” for clout

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

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

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

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

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

    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_ 3 месяца назад

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

  • @gokulkrishh
    @gokulkrishh 3 месяца назад +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.

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

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

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

      You earned a cookie for being so intelligent and knowledgeable