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

Not able to get next.config.js environment variable #206

Closed
rishabhchauhan421 opened this issue Mar 5, 2024 · 2 comments
Closed

Not able to get next.config.js environment variable #206

rishabhchauhan421 opened this issue Mar 5, 2024 · 2 comments

Comments

@rishabhchauhan421
Copy link

rishabhchauhan421 commented Mar 5, 2024

Hi, I want typesafe my environment variable which is correctly working for .env file. But I have saved some env variables inside next.config.js also. How can I typesafe my next.config.js env variable also.

next.config.js file

/** @type {import('next').NextConfig} */
const nextConfig = {
  env: {
    siteName: 'The Custom Crow',
    facebookUrl: 'https://www.facebook.com/drippy.in/',
    twitterUrl: 'https://twitter.com/drippy_in',
    pinterestUrl: 'https://www.pinterest.com/drippy_in/',
    linkedinUrl: 'https://www.linkedin.com/company/drippy-in/',
    youtubeUrl: 'https://www.youtube.com/channel/UC2sDh9f6i7hjZx2i0rY3N8Q',
  },
  reactStrictMode: false,
};

module.exports = nextConfig;

env.ts file

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

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
  },
  
  client: {
    NEXT_PUBLIC_BASE_URL: z.string().url(),
  },
  
  runtimeEnv: {
    // From .env file
    DATABASE_URL: process.env.DATABASE_URL,

    // From next.config.js, but it is showing error here
    siteName: process.env.siteName,
    facebookUrl: process.env.facebookUrl,
    twitterUrl: process.env.twitterUrl,
  },
});
@juliusmarminge
Copy link
Member

juliusmarminge commented Mar 13, 2024

i don't know when next.js loads these into the environment so perhaps your best bet is to set them as shared variables in env.ts instead?

// env.ts
import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
  },
  
  client: {
    NEXT_PUBLIC_BASE_URL: z.string().url(),
  },

  shared: {
    siteName: z.string(),
  },
  
  runtimeEnv: {
    DATABASE_URL: process.env.DATABASE_URL,
    siteName: "The Custom Crow",
  },
});

@rishabhchauhan421
Copy link
Author

Resolved:

inside env.ts

export const env = createEnv({
  server: {
    DATABASE_URL: z.string().url(),
  },
  
  client: {
    NEXT_PUBLIC_BASE_URL: z.string().url(),
    NEXT_PUBLIC_siteName: z.string().min(1),
    NEXT_PUBLIC_facebookUrl: z.string().min(1),
    NEXT_PUBLIC_twitterUrl: z.string().min(1),
  },
  
  runtimeEnv: {
    // From .env file
    DATABASE_URL: process.env.DATABASE_URL,

    // From next.config.js, but it is showing error here
    NEXT_PUBLIC_siteName: process.env.siteName,
    NEXT_PUBLIC_facebookUrl: process.env.facebookUrl,
    NEXT_PUBLIC_twitterUrl: process.env.twitterUrl,
  },
});

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

2 participants