t3-oss / t3-env

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

how to validate schema on build on next.config.ts #281

Open fahmiidris opened 1 week ago

fahmiidris commented 1 week ago

Next.js config now support .ts, and in this documentation https://env.t3.gg/docs/nextjs#validate-schema-on-build-(recommended) need jiti to load .ts on next.config.mjs.

does this need to be updated? and how to do it correctly and cleanly?

jferrettiboke commented 1 week ago

Yeah, I'm interested in this too.

I'm trying something like the following, but it doesn't seem to work.

import { env as clientEnv } from "@/lib/env/client";
import { env as serverEnv } from "@/lib/env/server";

// ...
chungweileong94 commented 1 week ago

I already submitted a PR for this, so basically, it is as simple as just importing the env file in next.config.ts.

import type { NextConfig } from "next";

// Import env here to validate during build.
import "./app/env";

const nextConfig: NextConfig = {
  /** ... */
};
jferrettiboke commented 1 week ago

@chungweileong94 I am already importing it into my next.config.ts file and it doesn’t work for me. See my previous comment.

I am using Next.js 15 canary with the turbo flag.

chungweileong94 commented 1 week ago

@chungweileong94 I am already importing it into my next.config.ts file and it doesn’t work for me. See my previous comment.

I am using Next.js 15 canary with the turbo flag.

Can you provide a small repo for it?

phl23 commented 1 week ago

@jferrettiboke I'm using Next,js 15 with turbo and just importing, as @chungweileong94 said, worked for me.

I renamed next.config.js to next.config.ts and env.js to env.ts. (the latter is not necessary here, I think)

In next.config.ts: replace this: await import("./src/env.js"); with this: import "./src/env"; Check that we don't use import() here!

Of course you use own path like ./app or else.

jferrettiboke commented 6 days ago

Thanks @chungweileong94 @phl23! I was importing in another way. It now works for me.

Before:

import type { NextConfig } from "next";
import { env as clientEnv } from "@/lib/env/client";
import { env as serverEnv } from "@/lib/env/server";

const nextConfig: NextConfig = {
  /* config options here */
};

export default nextConfig;

After:

import type { NextConfig } from "next";
import "@/lib/env/client";
import "@/lib/env/server";

const nextConfig: NextConfig = {
  /* config options here */
};

export default nextConfig;