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

question: how to use env vars in next.config ? #285

Open
tresorama opened this issue Nov 13, 2024 · 0 comments
Open

question: how to use env vars in next.config ? #285

tresorama opened this issue Nov 13, 2024 · 0 comments

Comments

@tresorama
Copy link

I need env vars to conditionally define properties in the nextConfig.

Without t3-env I'm doing this

// in next.config.mjs

/** @type {import('next').NextConfig} */
const nextConfig = {
  ...(process.env.NEXT_PUBLIC_APP_BUILD_MODE === 'static-export' && ({
    output: 'export',
  })),
  ...(process.env.NEXT_PUBLIC_APP_BUILD_MODE === 'api' && ({
    async headers() {
      return [ ... ];
    },
  })),
};

export default nextConfig;

Then following t3-env docs I have this:

// in next.config.mjs

import { fileURLToPath } from "node:url";
import createJiti from "jiti";
const jiti = createJiti(fileURLToPath(import.meta.url));
 
// Import env here to validate during build. Using jiti we can import .ts files :)
jiti("./app/env");
 

/** @type {import('next').NextConfig} */
const nextConfig = {
  // HOW TO REWRITE HERE ?
  // This file is a mjs file so i cannot import env vars from `app/env.ts`
  ...(process.env.NEXT_PUBLIC_APP_BUILD_MODE === 'static-export' && ({
    output: 'export',
  })),
  ...(process.env.NEXT_PUBLIC_APP_BUILD_MODE === 'api' && ({
    async headers() {
      return [ ... ];
    },
  })),
};

export default nextConfig;

Is this scenario possible ?
Or I need to convert app/env.ts to js ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant