DEV Community

Uncaught ReferenceError: process is not defined

Fred B. on August 20, 2023

How to solve the following error ? Uncaught ReferenceError: process is not defined Enter fullscreen mode Exit fulls...
Collapse
 
branwell profile image
Amanda Branwell

You, sir, are a lifesaver! (Also created an account just to say that.)

Collapse
 
soongle profile image
SOONG-E • Edited

you are a genius

Collapse
 
goldmaneitor profile image
Alvaro

i'd created this account just to tell you: u r my f*cking hero

Collapse
 
shanganegoda profile image
shanganegoda

Seems like "process.env" is removed, have to use "import.meta.env" instead.
So instead of process.env.VITE_SOME_KEY -> import.meta.env.VITE_SOME_KEY

Read more here - vitejs.dev/guide/env-and-mode.html

Collapse
 
timrohrer profile image
tim.rohrer

I suspect this is the actual solution, but I can't say as I understand why VITE deviates from the standard approach.

Collapse
 
agustingonzalezdev profile image
AgustinGonzalezDev

Graciass geniooooooooooooooooooooooooo!!!

Collapse
 
frank_garcia profile image
Frank Garcia • Edited

Just do it the Vite way and forget all of this!

  1. Add your .env files e.g. ".env.testing".
  2. Use "VITE_" as the prefix for the variables instead of "REACT_APP_" e.g. "VITE_SOME_NAME".
  3. Use "import.meta.env" instead of "process.env" e.g. "import.meta.env.VITE_SOME_NAME".

Done.

Collapse
 
mukwaba_johnbaptistmed profile image
Mukwaba John Baptist (Media-vybz)

on my side, loadEnv was throwing an error then i revised my code s below. things worked perfectly. thanks for the clue i was totally lost

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import dotenv from 'dotenv';

dotenv.config(); // Load environment variables from .env

// vitejs.dev/config/
export default defineConfig(({ mode }) => {
return {
define: {
'process.env.REACT_APP_BASE_URL': JSON.stringify(process.env.REACT_APP_BASE_URL),
},
plugins: [react()],
};
});

Collapse
 
seven profile image
Caleb O.

Thank you for writing this!

Collapse
 
zeeshansafdar48 profile image
Zeeshan Safdar

Thanks a lot, it worked like a charm.

Collapse
 
smartdriver321 profile image
smartdriver321

wow, you saved my time bruh

Collapse
 
helberan profile image
helberan

Thank you so much!!! ❤️

Collapse
 
uchestan profile image
UnderDog Developments

Thanks a lot

Collapse
 
pagarevijayy profile image
Vijay Pagare

worked on local but fails when deploying the project to vercel. any help?

Collapse
 
deptrai95 profile image
Duc Anh Nguyen

Thank you Bro!
That helped me!!!

Collapse
 
olanre profile image
Princess Lanre

Thank you so much

Collapse
 
carlosmontesi profile image
carlos-montes-i

Thanks a lot!
You help me too much!

Collapse
 
christi4ncordei profile image
christian

I've been looking for this shit all over the internet and you're the one who saved me, thanks bro

Collapse
 
dimitarsergeev profile image
Dimitar Sergeev

i'd also created this account just to tell you: u r my f*cking hero

Collapse
 
skr_fa042d1d718cdaff3db92 profile image
SKR

very helpful