t3-oss / t3-env

https://env.t3.gg
MIT License
2.45k stars 79 forks source link

Question: How to use with Expo #203

Closed FleetAdmiralJakob closed 3 months ago

FleetAdmiralJakob commented 4 months ago

I saw that this library supports expo but does not know how to implement it.

drazik commented 4 months ago

I don't know if I'm doing this right, but here is what I did and is working:

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

export const env = createEnv({
    clientPrefix: "EXPO_PUBLIC_",
    client: {
        EXPO_PUBLIC_GOOGLE_MAPS_API_KEY: z.string().min(1),
        EXPO_PUBLIC_API_BASE_URL: z.string().url(),
    },
    runtimeEnv: {
        EXPO_PUBLIC_GOOGLE_MAPS_API_KEY:
            process.env.EXPO_PUBLIC_GOOGLE_MAPS_API_KEY,
        EXPO_PUBLIC_API_BASE_URL: process.env.EXPO_PUBLIC_API_BASE_URL,
    },
})

Then use the env object

import { env } from "@/env"

env.EXPO_PUBLIC_GOOGLE_MAPS_API_KEY

I hope it helps!

tyeetale commented 3 months ago

also, I found a typescript issue when importing, so I added:

"@t3-oss/env-core": ["./node_modules/@t3-oss/env-core/dist/index.d.ts"]

to the paths section in tsconfig.json:

{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true,
    "module": "ESNext",
    "paths": {
      "@t3-oss/env-core": ["./node_modules/@t3-oss/env-core/dist/index.d.ts"]
    }
  },
  "include": ["**/*.ts", "**/*.tsx"]
}
juliusmarminge commented 3 months ago

also, I found a typescript issue when importing, so I added:

That's most likely related to a bad moduleResolution setting in your tsconfig (or the expo base config you're extending from) See #189, #190, #191, #195 etc