Тёмный
No video :(

DON'T USE Environment Variables Without This 

James Q Quick
Подписаться 203 тыс.
Просмотров 21 тыс.
50% 1

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...

Опубликовано:

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 36   
@dechobarca
@dechobarca 6 месяцев назад
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 6 месяцев назад
+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 6 месяцев назад
​@@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 5 месяцев назад
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 6 месяцев назад
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 6 месяцев назад
Let's install some packages and write a bunch of code to access and validate the types of our env variables that we control.
@akvirus2
@akvirus2 5 месяцев назад
or you can use type augmentation to define your types. use zod for form validation or other stuff
@gosnooky
@gosnooky 6 месяцев назад
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.
@kiikoh
@kiikoh 6 месяцев назад
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 6 месяцев назад
You can also just make global types like this: declare global { namespace NodeJS { interface ProcessEnv { DB_CONNECTION: string; DB_NAME: string; } } }
@davidtran3237
@davidtran3237 6 месяцев назад
10:57 how would you go about handling the validation at build time?
@wobsoriano
@wobsoriano 6 месяцев назад
Ill do tsx env.ts && npm run build
@gagiknavasatariyan7316
@gagiknavasatariyan7316 6 месяцев назад
This was really helpful for me, Thanks!
@JamesQQuick
@JamesQQuick 6 месяцев назад
So glad to hear that. Thanks for watching!!
@gokulkrishh
@gokulkrishh 6 месяцев назад
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.
@asteinerd
@asteinerd 6 месяцев назад
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
@technobanjo
@technobanjo 6 месяцев назад
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.
@gleitonfranco1260
@gleitonfranco1260 6 месяцев назад
I use IntelliJ btw ;-)
@yakimovev
@yakimovev 6 месяцев назад
Really useful video, absolute like😃
@FreeCodeArena
@FreeCodeArena 6 месяцев назад
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.
@Buy_YouTube_Views_a135
@Buy_YouTube_Views_a135 6 месяцев назад
Love this improvement
@ayushshende4290
@ayushshende4290 6 месяцев назад
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 6 месяцев назад
You can use the property transform from zod
@xdneos
@xdneos 6 месяцев назад
Even you can use regex to be sure that the property only contain Numbers
@CharcoalDaddyBBQ
@CharcoalDaddyBBQ 5 месяцев назад
But I set my environment variables.... I don't see the point of all this extra code...
@JamesQQuick
@JamesQQuick 5 месяцев назад
Do you not see value in intellisense and validation for those variables?
@jonathangamble
@jonathangamble 6 месяцев назад
use Valibot, way less overhead and has treeshaking
@Danielo515
@Danielo515 6 месяцев назад
Zod was a nice proof of concept, but nobody should be using it by now.
@_neuromanser_
@_neuromanser_ 6 месяцев назад
Yes, add another dependency to project just to check env variable… 🙄
@anasouardini
@anasouardini 6 месяцев назад
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 ¯\_(ツ)_/¯
@IvanRandomDude
@IvanRandomDude 6 месяцев назад
We are seriously at the point where even using env variables is "hard"
@YouTubePL666
@YouTubePL666 5 месяцев назад
Just RU-vidrs making this “hard” for clout
@Danielo515
@Danielo515 6 месяцев назад
This may be an impressive revelation... For those with 0 experience programming
@zapfska7390
@zapfska7390 6 месяцев назад
You earned a cookie for being so intelligent and knowledgeable
@immikegt
@immikegt 7 дней назад
And if just use ‘${process.env.EMAIL}’ ? The error disappeared and works.
Далее
The TRUTH About TypeScript Enums
12:04
Просмотров 7 тыс.
I Found a BETTER Way to Do File Uploads
8:30
Просмотров 15 тыс.
10 common mistakes with the Next.js App Router
20:37
Просмотров 209 тыс.
VSCode is Not Enough Anymore in 2024
3:21
Просмотров 19 тыс.
STOP Using JavaScript For These 5 Things!
8:05
Просмотров 17 тыс.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Only The Best Developers Understand How This Works
18:32
Learn TypeScript Generics In 13 Minutes
12:52
Просмотров 260 тыс.