t3-oss / create-t3-app

The best way to start a full-stack, typesafe Next.js app
https://create.t3.gg
MIT License
24.89k stars 1.15k forks source link

bug: Enviroment Variables not working #999

Closed gutrapp closed 1 year ago

gutrapp commented 1 year ago

Provide environment information

error - Failed to load env from D:\projects\imdb-clone.env TypeError: Cannot read properties of undefined (reading 'split')

❌ Invalid environment variables: DATABASE_URL: Required NEXTAUTH_URL: Required EMAIL_SERVER: Required EMAIL_FROM: Required

const _serverEnv = serverSchema.safeParse(process.env); console.log(serverSchema.safeParse(process.env)) { success: false, error: ZodError: [ { "code": "invalid_type", "expected": "string", "received": "undefined", "path": [ "DATABASE_URL" ], "message": "Required" }, { "code": "invalid_type", "expected": "string", "received": "undefined", "path": [ "NEXTAUTH_URL" ], "message": "Required" }, { "code": "invalid_type", "expected": "string", "received": "undefined", "path": [ "EMAIL_SERVER" ], "message": "Required" }, { "code": "invalid_type", "expected": "string", "received": "undefined", "path": [ "EMAIL_FROM" ], "message": "Required" } ]

Describe the bug

Somehow the program ins't reading the .env variables and saying that they are "Required" even tho I had pu them all with values on the .env and on the .env.example. Then I did a quick console.log on the schema after it's been checked and got that, and there it said it got undefined. I don't understand why it can't read the variables, and the weirdest part is that this is the first time this is happening, my first project I used Instagram auth and it worked flawlessly. Just a reminder I done everything correctly, placed the EMAIL_SERVER and EMAIL_FROM on the and their respective values on the .env file, so I don't undersantd why it isn't working properly. And it gets even weirder because when I push the database it works? So what im trying to say is that it is reading the .env file but at the same time it isn't? Because prisma is connecting to the database.Can someone please help

To reproduce

To be honest I could even tell you how to reproduce because I didn't do anything out of the ordinary, create the t3 template changed from discord provider to email, pushed the prisma database, then when i tried to do run the program, it just didn't work.

Additional information

No response

circles-00 commented 1 year ago

Hello @gutrapp, I've been using t3 stack for a while now and I'm willing to try helping you on this.

I will ask you a couple of questions, just to make sure you're doing everything properly:

gutrapp commented 1 year ago

Hi thanks for reaching out:

circles-00 commented 1 year ago
  • Did you hard restart your server after adding your new environment variables to the .env file? If you mean a server on Vercel, no I didn't because im not using it, but after adding the .env variables I always saved everything got the same error

I meant the dev server, so after adding your environment variables to the .env file, did you stop your dev server completely and run it again from the terminal?

Every time you add a new environment variable, you need to restart the server completely so the environment variables get loaded properly.

Also it would be very helpful if you can create a minimal repository that can reproduce the issue that you're seeing.

gutrapp commented 1 year ago

I got it to work, i don't know how but it works now. I just closed now it works again, maybe just a vscode glitch or idk thanks for trying to help tho, appreciate

iduuck commented 1 year ago

I have/had the same problem. The problem comes from using the environment variable setup from t3 in your middleware. The middleware is also replacing the process.env in the middleware, hence, we are not able to use the server variables.


Edit: The issue describes this pretty well: https://github.com/vercel/next.js/issues/30612#issuecomment-955065804