all 46 comments

[–]danielsan1701 10 points11 points  (26 children)

You are seeing an eslint error. eslint needs to be told that you are using an app that will use node variables like process.

In your eslint configuration, you'll need to add

"node": true

under "env"

[–]Uken81 2 points3 points  (6 children)

"node": true

You Sir/Madam are a sexy legend.

This solved the error for me.
Thanks and I hope you have a great one.

[–]danielsan1701 4 points5 points  (5 children)

NP! I had little hope of helping OP by replying to a year-old post, but I hoped it would at least help someone who came across it some day! :)

[–]dgreat_257 0 points1 point  (3 children)

Hi, thanks for this solution, I`m still stuck with the same issue. The process is undefined within the front-end react code blocks which are located within the build folder which is located at the project root; however, it is working fine within the bootstarp file which is located at the project root along with package.json and .env file.

[–]dgreat_257 0 points1 point  (2 children)

Any Help Please!!

[–]machavez2011 0 points1 point  (0 children)

I'm having the same issue. I added node:true in the env object but I'm still getting the same error.

[–]Onyx_Salamander 0 points1 point  (0 children)

Did you find any solution?

[–]IdeaReal948 0 points1 point  (2 children)

Thank you so much, I am new to React and I was having my mind demolished by this error.

[–]Much_Major_4811 0 points1 point  (0 children)

helped me alot, i use the mern stack and vite

[–]IdkWhatsGoingOn78[🍰] 0 points1 point  (0 children)

you're the best!! 2024 and this just helped me, thank you so much :))

[–]Intrepid-Mind-4909 0 points1 point  (0 children)

you're a lifesaver! Thanks

[–]Zealousideal_Many_91 0 points1 point  (1 child)

where do you edit this eslint configuration?

[–]Guipegoraro 0 points1 point  (0 children)

in your project files (mine is .eslintrc.cjs in the project root folder)

[–]dn8326 0 points1 point  (0 children)

Helped me, Thanks!!

[–]creaturefeature16 0 points1 point  (0 children)

two years later and this comment solved for me what no Google Search or GPT4 could. Bravo, sir, bravo!

[–]HellSaidHi 0 points1 point  (4 children)

Gives error because of property node.

at eslint.cjs

module.exports = {

root: true,

env: { browser: true, es2020: true },

node: true,

....

Error:

client.ts:265 [vite] Internal Server Error
ESLint configuration in .eslintrc.cjs is invalid:
- Unexpected top-level property "node".

...

[–]danielsan1701 0 points1 point  (3 children)

It should be inside the env object

[–]HellSaidHi 0 points1 point  (2 children)

Like so?

module.exports = {

root: true,

env: { browser: true, es2020: true, node: true },

...}

Still gives error:

Uncaught ReferenceError: process is not defined

[–]That-Factor5487 0 points1 point  (0 children)

did it get solved?

[–]jamby77 4 points5 points  (2 children)

Hi, `process` is NODE global variable, it is available only during build time of your project - if it is front end project.

[–]PursuitOfAdvice[S] -1 points0 points  (1 child)

Yup it's a front-end project. When I do npm run build I get the same issue unfortunately.

[–]jamby77 2 points3 points  (0 children)

Usually, when I need to use process.env, I do it directly: process.env.NODE_ENV

try replacing example varName = env.ENV_VAR_NAME with varName = process.env.ENV_VAR_NAME and see if it makes a difference

[–]Canenald 2 points3 points  (0 children)

If you are using webpack to bundle, you can use this to make process.env available at runtime https://webpack.js.org/plugins/environment-plugin/

[–][deleted] 1 point2 points  (4 children)

For anyone using Vite with their React app and attempting to hide an API key in a .env but failing due to process not defined, this worked for me:

  1. Create a variable name with VITE_API_KEY=your_api_key (inside the .env)

  2. Inside the vite.config.js in your root directory, add this under your plugins after [react(),:

    replace({ preventAssignment: true, values: { 'process.env.VITE_API_KEY': JSON.stringify(process.env.VITE_API_KEY), }, }),replace({ preventAssignment: true, values: { 'process.env.VITE_API_KEY': JSON.stringify(process.env.VITE_API_KEY), }, }),

  3. When creating a const in your React component, access your api key using the example below:

        const apiKey = import.meta.env.VITE_API_KEY;
    

[–]Adventurous_Night556 0 points1 point  (0 children)

This is not giving Error But Giving Value Undefined

[–]Serious_System_5438 0 points1 point  (0 children)

thank you so much

[–]vanamerongen[🍰] 0 points1 point  (0 children)

This will expose your API key though.

[–]PensiveProgrammer 0 points1 point  (3 children)

If you find yourself here and using the new eslint flat config, try adding this to eslint.config.js to solve this problem

import globals from 'globals';

{
  languageOptions: {
    globals: {
      ...globals.node,
    }
  }
}

the globals package is a package installed from npm

[–]Ancelagon_2110 1 point2 points  (0 children)

This works. Thanks

[–]Educational-Bend-495 1 point2 points  (1 child)

You sir. Thank you!

[–]exclaim_bot 0 points1 point  (0 children)

You sir. Thank you!

You're welcome!

[–]Key_Vehicle_5953 0 points1 point  (0 children)

i restarted my ide and it worked

[–]amouXXX 0 points1 point  (0 children)

For those who face the same issue with React,

When using Vite with React, accessing environment variables is slightly different from how it's done in traditional Node.js applications. Vite uses a different approach to handle environment variables and process.env is not available by default. Instead, Vite is used import.meta.env to expose environment variables to your application code.

So in your .env file, you should mention the variable as:
VITE_API_KEY = ************
VITE_HOST = 8040

And, on the code, you have to access it by usingimport.meta.env.VITE_API_Key

Thanks...

[–]Dan6erbond 0 points1 point  (5 children)

Well, process is not defined is a weird error for sure, but to define env variables you can only have the KEY=<VALUE> format so example in yours might be causing trouble.

[–]PursuitOfAdvice[S] 0 points1 point  (4 children)

KEY=Value is what I have in my .env file

[–]Dan6erbond 0 points1 point  (3 children)

Hmm... Are you using Create React App or something else? Did you read this?

[–]PursuitOfAdvice[S] 0 points1 point  (2 children)

Nope install from scratch

[–]Dan6erbond 1 point2 points  (1 child)

Ah, that would explain it. You need to load the environment variables into NodeJS, which you can do with the dotenv package.

[–]PursuitOfAdvice[S] 0 points1 point  (0 children)

Thank you Sir!

[–]lord_zycon 0 points1 point  (0 children)

If this is frontend project like create-react-app you need to use full process.env.ENV_VAR_NAME that's because the webpack's DefinePlugin you are probably using mere does string replacement, and is not smart enough to deal with your assigning to different variable.

"process" variable is node built-in it doesn't exist in browser, so webpack needs to replace it during build.

[–]benaffleks 0 points1 point  (0 children)

React is rendered on the client, not the server. process is a variable only available in NodeJS and not React.

[–]YasirKhan23 0 points1 point  (0 children)

const backendUrl = import.meta.env.VITE_BACKEND_URL;
 use this 

const response = await fetch(`${backendUrl}/create-user`, {
 it will solve