Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make env vars optional #585

Closed
bobbyconnolly opened this issue Oct 7, 2022 · 10 comments · Fixed by #587
Closed

feat: make env vars optional #585

bobbyconnolly opened this issue Oct 7, 2022 · 10 comments · Fixed by #587
Labels
🌟 enhancement New feature or request

Comments

@bobbyconnolly
Copy link

Is your feature request related to a problem? Please describe.

I pulled my t3 app on my laptop at an internet cafe (I wanted to do a little UI work). Trying to run it locally, I'm confronted with
❌ Invalid environment variables:
DATABASE_URL: Required
NEXTAUTH_SECRET: Required
NEXTAUTH_URL: Required
DISCORD_CLIENT_ID: Required
DISCORD_CLIENT_SECRET: Required

I created a .env and stuck it in my src folder with those variables with empty values

Still doesn't work.

I then added optional to them here

export const serverSchema = z.object({
    DATABASE_URL: z.string().url().optional(),
    NODE_ENV: z.enum(["development", "test", "production"]),
    NEXTAUTH_SECRET: z.string().optional(),
    NEXTAUTH_URL: z.string().url().optional(),
    DISCORD_CLIENT_ID: z.string().optional(),
    DISCORD_CLIENT_SECRET: z.string().optional(),
})

And that fixed the problem.

Describe the solution you'd like to see

I believe these env vars should be optional from the start. People who are just starting out my just want to be focusing on their frontends and not have to worry about environment issues.

Desribe alternate solutions

You probably have a better solution than the one I proposed.

Just make source buildable out-of-the-box if someone were to push to their remote, then pull to another machine.

Additional information

No response

@bobbyconnolly bobbyconnolly added the 🌟 enhancement New feature or request label Oct 7, 2022
@bobbyconnolly
Copy link
Author

bobbyconnolly commented Oct 7, 2022

Actually, I'm still having issues when I try to build

  Type 'undefined' is not assignable to type 'string'.

  21 |     providers: [
  22 |         DiscordProvider({
> 23 |             clientId: env.DISCORD_CLIENT_ID,
     |             ^
  24 |             clientSecret: env.DISCORD_CLIENT_SECRET,
  25 |         }),
  26 |         // ...add more providers here

@bobbyconnolly
Copy link
Author

I put random values into my .env, and now I'm getting

Type error: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

  21 |     providers: [
  22 |         DiscordProvider({
> 23 |             clientId: env.DISCORD_CLIENT_ID,
     |             ^
  24 |             clientSecret: env.DISCORD_CLIENT_SECRET,
  25 |         }),
  26 |         // ...add more providers here

I realize this is because I made those objects optional so I reverted schema.mjs

Now I'm facing

warn  - You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
❌ Invalid environment variables:
 DATABASE_URL: Required
 NEXTAUTH_SECRET: Required
 NEXTAUTH_URL: Required
 DISCORD_CLIENT_ID: Required
 DISCORD_CLIENT_SECRET: Required

error - Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error

> Build error occurred
Error: Invalid environment variables
    at file:https:///C:/Users/conno/source/repos/bobbyconnolly/ginger/src/env/server.mjs:16:11
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async Object.loadConfig [as default] (C:\Users\conno\source\repos\bobbyconnolly\ginger\node_modules\next\dist\server\config.js:68:36)
    at async Span.traceAsyncFn (C:\Users\conno\source\repos\bobbyconnolly\ginger\node_modules\next\dist\trace\trace.js:79:20)       
    at async C:\Users\conno\source\repos\bobbyconnolly\ginger\node_modules\next\dist\build\index.js:69:28
    at async Span.traceAsyncFn (C:\Users\conno\source\repos\bobbyconnolly\ginger\node_modules\next\dist\trace\trace.js:79:20)       
    at async Object.build [as default] (C:\Users\conno\source\repos\bobbyconnolly\ginger\node_modules\next\dist\build\index.js:65:29)

with env vars /src/.env

 DATABASE_URL=localhost:5432/your_database_name
 NEXTAUTH_SECRET=taco
 NEXTAUTH_URL=https://localhost:3000
 DISCORD_CLIENT_ID=taco
 DISCORD_CLIENT_SECRET=password

@bobbyconnolly
Copy link
Author

I see now that server.mjs / line 10 (const _serverEnv = serverSchema.safeParse(process.env)) is actually grabbing the environment variables.

I finally had the sense to make another npx create-t3-app@latest and noticed that the .env lives one folder up from /src with the following data

# When adding additional env variables, the schema in /env/schema.mjs should be updated accordingly

# Prisma
DATABASE_URL=file:./db.sqlite

# Next Auth
NEXTAUTH_SECRET=
NEXTAUTH_URL=https://localhost:3000

# Next Auth Discord Provider
DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=

Now building works and yarn dev working 🥳

@bobbyconnolly
Copy link
Author

Perhaps the solution is to improve the docks slightly about what to do if you pull to a different machine

Thanks!

@juliusmarminge
Copy link
Member

Yes .env should be in the project root.

Maybe we should have a committed version of .env-example with the default values so that one can copy those if they pull the repo from remote?

@c-ehrlich c-ehrlich mentioned this issue Oct 7, 2022
3 tasks
@pablojsx
Copy link

I'm getting the same error, just following Theo's tutorial on youtube:

`

❌ Invalid environment variables: {

06:37:07.637 | NEXTAUTH_SECRET: [ 'Required' ],
06:37:07.637 | DISCORD_CLIENT_ID: [ 'Required' ],
06:37:07.637 | DISCORD_CLIENT_SECRET: [ 'Required' ]
06:37:07.637 | }
06:37:07.637 | - error Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error
06:37:07.643 |  
06:37:07.643 | > Build error occurred
06:37:07.644 | Error: Invalid environment variables
06:37:07.644 | at T.m (file:https:///vercel/path0/node_modules/@t3-oss/env-nextjs/dist/index.mjs:1:500)
06:37:07.644 | at T (file:https:///vercel/path0/node_modules/@t3-oss/env-nextjs/dist/index.mjs:1:690)
06:37:07.644 | at P (file:https:///vercel/path0/node_modules/@t3-oss/env-nextjs/dist/index.mjs:1:1081)
06:37:07.644 | at file:https:///vercel/path0/src/env.mjs:4:20
06:37:07.644 | at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
06:37:07.663 | Error: Command "npm run build" exited with 1

`

@aureliopetrone
Copy link

What about postgresql where DATABASE_URL is not needed? It should be optional, isn't it?

@c-ehrlich
Copy link
Member

why would a connection URL not be needed for postgres?

@aureliopetrone
Copy link

aureliopetrone commented Oct 15, 2023

@c-ehrlich because it uses the variable POSTGRES_URL, etc

@juliusmarminge
Copy link
Member

@c-ehrlich because it uses the variable POSTGRES_URL, etc

then you should change your env object to use POSTGRES_URL instead of DATABASE_URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants