@natu/env

💻 Environment Variables handling

T3 Env ↗ (opens in a new tab) is a library that provides environmental variables checking at build time, type validation and transforming. It ensures that your application is using the correct environment variables and their values are of the expected type. You’ll never again struggle with runtime errors caused by incorrect environment variable usage.

Config file is located at env.mjs. Simply set your client and server variables and import env from any file in your project.

export const env = createEnv({
  server: {
    // Server variables
    SECRET_KEY: z.string(),
  },
  client: {
    // Client variables
    API_URL: z.string().url(),
  },
  runtimeEnv: {
    // Assign runtime variables
    SECRET_KEY: process.env.SECRET_KEY,
    API_URL: process.env.NEXT_PUBLIC_API_URL,
  },
});

If the required environment variables are not set, you'll get an error message:

   Invalid environment variables: { SECRET_KEY: [ 'Required' ] }

🏗️ Validate schema on build

Import your newly created file in your next.config.mjs. This will make sure your environment variables are validated at build time which will save you a lot of time and headaches down the road.

import '@natu/env/src/env/env.mjs';
 
/** @type {import("next").NextConfig} */
const config = {
  /** ... */
};
 
export default config;