t3-oss / t3-env

https://env.t3.gg
MIT License
2.76k stars 86 forks source link

Vercel Server environment variables being populated #249

Closed jacobsamo closed 4 months ago

jacobsamo commented 4 months ago

Pages to Investigate

https://github.com/t3-oss/t3-env https://github.com/jacobsamo/Mixie/tree/dev

Steps to Reproduce

I am currently using using T3 Env for type safe environment variables however on vercel it seems that the server environment variables aren't pulling in for some reason. it builds fine in locally and builds fine using the Vercel CLI using vercel pull and vercel build.

I have checked that my environment variables are correct in my settings and they are and are set for the right environments. This is the error I see:

mixie/website:build: $ next build
@mixie/website:build: ❌ Invalid environment variables: {
@mixie/website:build:   SUPABASE_URI: [ 'Required' ],
@mixie/website:build:   UNSPLASH_SECRET: [ 'Required' ],
@mixie/website:build:   UPLOADTHING_SECRET: [ 'Required' ],
@mixie/website:build:   UPLOADTHING_APP_ID: [ 'Required' ],
@mixie/website:build:   SUPABASE_SERVICE_ROLE: [ 'Required' ],
@mixie/website:build:   GOOGLE_AI_API_KEY: [ 'Required' ],
@mixie/website:build:   UPSTASH_REDIS_REST_URL: [ 'Required' ],
@mixie/website:build:   UPSTASH_REDIS_REST_TOKEN: [ 'Required' ]
@mixie/website:build: }
@mixie/website:build:  ⨯ Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error
@mixie/website:build: 
@mixie/website:build: > Build error occurred
@mixie/website:build: Error: Invalid environment variables
@mixie/website:build:     at onValidationError (file:///vercel/path0/node_modules/@t3-oss/env-core/dist/index.js:29:15)
@mixie/website:build:     at createEnv (file:///vercel/path0/node_modules/@t3-oss/env-core/dist/index.js:35:16)
@mixie/website:build:     at createEnv (file:///vercel/path0/node_modules/@t3-oss/env-nextjs/dist/index.js:12:12)
@mixie/website:build:     at file:///vercel/path0/apps/website/env.mjs:4:20
@mixie/website:build:     at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
@mixie/website:build:     at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
@mixie/website:build:     at async loadConfig (/vercel/path0/node_modules/next/dist/server/config.js:698:36)

What else have I tried

I tired removing the package and running with normal process.env however still no luck. environment variables are still not being populated

My config:

import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

export const env = createEnv({
  server: {
    NODE_ENV: z
      .enum(["development", "test", "production"])
      .default("development"),
    UNSPLASH_SECRET: z.string().min(1),
    UPLOADTHING_SECRET: z.string().min(1),
    UPLOADTHING_APP_ID: z.string().min(1),
    SUPABASE_SERVICE_ROLE: z.string().min(1),
    GOOGLE_AI_API_KEY: z.string().min(1),
    UPSTASH_REDIS_REST_URL: z.string().min(1),
    UPSTASH_REDIS_REST_TOKEN: z.string().min(1),
    RESEND_API_KEY: z.string().min(1),
  },
  client: {
    NEXT_PUBLIC_API_APP_TOKEN: z.string().min(1),
    NEXT_PUBLIC_APP_URL: z.string().url(),
    NEXT_PUBLIC_UNSPLASH_ACCESS_KEY: z.string().min(1),
    NEXT_PUBLIC_SUPABASE_URL: z.string().min(1),
    NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string().min(1),
    NEXT_PUBLIC_POSTHOG_KEY: z.string().min(1),
    NEXT_PUBLIC_POSTHOG_HOST: z.string().min(1),
  },
  runtimeEnv: {
    // Server
    NODE_ENV: process.env.NODE_ENV,
    RESEND_API_KEY: process.env.RESEND_API_KEY,
    UNSPLASH_SECRET: process.env.UNSPLASH_SECRET,
    UPLOADTHING_SECRET: process.env.UPLOADTHING_SECRET,
    UPLOADTHING_APP_ID: process.env.UPLOADTHING_APP_ID,
    SUPABASE_SERVICE_ROLE: process.env.SUPABASE_SERVICE_ROLE,
    GOOGLE_AI_API_KEY: process.env.GOOGLE_AI_API_KEY,
    UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL,
    UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN,

    // Client
    NEXT_PUBLIC_APP_URL: process.env.NEXT_PUBLIC_APP_URL,
    NEXT_PUBLIC_API_APP_TOKEN: process.env.NEXT_PUBLIC_API_APP_TOKEN,
    NEXT_PUBLIC_UNSPLASH_ACCESS_KEY:
      process.env.NEXT_PUBLIC_UNSPLASH_ACCESS_KEY,
    NEXT_PUBLIC_SUPABASE_URL: process.env.NEXT_PUBLIC_SUPABASE_URL,
    NEXT_PUBLIC_SUPABASE_ANON_KEY: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
    NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST,
  },
});

Branches

nass59 commented 4 months ago

Hello @jacobsamo , Since turbo 2.0, Strict Mode for environment variables is now the default

You can try to declare your server env variables to your turbo.json config in globalEnv or env

For example:

{
  "$schema": "https://turborepo.org/schema.json",
  "globalEnv": [
    "NODE_ENV",
    "RESEND_API_KEY",
    "UNSPLASH_SECRET",
    "UPLOADTHING_SECRET",
    "UPLOADTHING_APP_ID",
    "SUPABASE_SERVICE_ROLE",
    "GOOGLE_AI_API_KEY",
    "UPSTASH_REDIS_REST_URL",
    "UPSTASH_REDIS_REST_TOKEN"
  ],
...
jacobsamo commented 4 months ago

Thank you so much, this ended up working, silly me for not trying that. spent way too much time trying to fix.