Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

388
Views
Access environment variables from client

I'm making a lot of API calls from within my components using fetch. Everytime I had to write this logic to determine which environment I'm in So, I created a Utils class which returns the correct base url:

export default class Utils {
  static get baseUrl() {
    const inDev = process.env.NODE_ENV !== 'production';
    const { NEXT_PUBLIC_DEV_URL, NEXT_PUBLIC_PROD_URL } = process.env;
    return inDev ? NEXT_PUBLIC_DEV_URL : NEXT_PUBLIC_PROD_URL;
  }
}

But now I'm getting this error when my component loads:

ReferenceError: process is not defined

I looked around and found that I need to mark my variables as NEXT_PUBLIC so I did but the problem still persists. What's the ideal way to handle this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to expose ENV variables to the client you can use publicRuntimeConfig inside your NextJS configuration (next.config.js). Here is how you could do it:

publicRuntimeConfig: {
  myEnvVar: process.env.MY_ENV_VAR
}

And then in the file you would like to use your ENV variable:

import getConfig from 'next/config';

const { publicRuntimeConfig } = getConfig();

const envVar = publicRuntimeConfig.myEnvVar // store in it a 'const' or do whatever you want with it,; 
about 4 years ago · Juan Pablo Isaza Report

0

Your class can actually look like this :-

export default class Utils {
  static get baseUrl() {
    return process.env.NEXT_PUBLIC_URL;
  }
}

The NEXT_PUBLIC prefix should work well for inlining the values which are replaced at build time and sent to the browser as per docs.

In your .env.development (or whatever your dev environment file is called) file, you can have NEXT_PUBLIC_URL pointing to http://localhost:8000 and in your production you will probably have production env variables setup using a GUI (like in Netlify or Vercel), so there NEXT_PUBLIC_URL can be https://blablasite.com.

Either way, NEXT_PUBLIC prefix will ensure that at build time those values are picked up and inlined before sending the same to browser.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!