create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Uncaught ReferenceError: process is not defined (NOT react-error-overlay)

Open EnoahNetzach opened this issue 2 years ago • 17 comments

Describe the bug

Using the optional chaining operator with process (process?.env) throws a ReferenceError stating that process is not defined.

I originally encountered this behaviour because a dependency is using that syntax, then I tried to write the same in my code and it still breaks.

Note that process.env works, and it doesn't appear to be a problem with react-error-overlay (I also tried to use this fix without success).

A workaround is to add the following in index.tsx:

if (!('process' in window)) {
  // @ts-ignore
  window.process = {}
}

Did you try recovering your dependencies?

I just freshly installed it from scratch using

yarn create react-app temp-app --template typescript

Which terms did you search for in User Guide?

https://github.com/facebook/create-react-app/issues?q=is%3Aissue+uncaught+referenceError%3A+process+is+not+defined https://github.com/facebook/create-react-app/issues?q=is%3Aissue+process+is+not+defined

Environment

Environment Info:

  current version of create-react-app: 5.0.0
  running from ~/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app

  System:
    OS: macOS 12.2.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.12.0 - ~/.nvm/versions/node/v16.12.0/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v16.12.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.12.0/bin/npm
  Browsers:
    Chrome: 99.0.4844.83
    Edge: Not Found
    Firefox: Not Found
    Safari: 15.3
  npmPackages:
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    react-scripts: 5.0.0 => 5.0.0 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Install CRA yarn create react-app temp-app --template typescript
  2. Add {process?.env?.NODE_ENV} in App.tsx (e.g. in the <p>)

Expected behavior

development is shown

Actual behavior

blank page, the following error in the browser console Screenshot 2022-03-28 at 09 39 42

Reproducible demo

https://github.com/EnoahNetzach/create-react-app-no-process

EnoahNetzach avatar Mar 28 '22 07:03 EnoahNetzach

I also encountered this. This is not a bug but intended behavior
https://stackoverflow.com/questions/41359504/webpack-bundle-js-uncaught-referenceerror-process-is-not-defined

. Downgrade to react-scripts@4 or just detach your project and add the config on webpack

masterbater avatar Mar 31 '22 20:03 masterbater

Thanks @masterbater for the answer, but that looks like a different issue, since cra does provide (some of) the env variables through webpack's DefinePlugin.

Indeed process.env.NODE_ENV does work, the issue I'm encountering is with the optional chaining operator.

EnoahNetzach avatar Apr 01 '22 07:04 EnoahNetzach

I am getting this error by simply using destructuring in an export.

export const {
  REACT_APP_ABC = "ABC",
  REACT_APP_XYZ = "XYZ",
} = process.env;

ghost avatar Apr 15 '22 23:04 ghost

It doesn't matter if you destructure or use optional chaining, this error just happens whenever you access process outside of a React component as far as I can see.

ghost avatar Apr 15 '22 23:04 ghost

I'm getting this too

Klingefjord avatar Sep 03 '22 22:09 Klingefjord

You can read how to set env for react. If the cause is a plugin fork it or ask the author to update it

On Sun, Sep 4, 2022, 6:14 AM Oliver Klingefjord, @.***> wrote:

I'm getting this too

— Reply to this email directly, view it on GitHub https://github.com/facebook/create-react-app/issues/12212#issuecomment-1236206060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGI6VFHSCQCRW7VIDQM4FO3V4PETJANCNFSM5R2IFIFQ . You are receiving this because you were mentioned.Message ID: @.***>

masterbater avatar Sep 04 '22 00:09 masterbater

this is so stupid... can not believe react team could make such mistake...

lovetingyuan avatar Sep 11 '22 17:09 lovetingyuan

We are trying to update some libraries and we are getting this issue. Tried to add this

  "overrides": {
    "react-error-overlay": "6.0.9"
  }

to package.json and do a clean install, but no changes. We cant update react-scripts as it will crash whole project. How is this an issue?

SourceCipher avatar Sep 12 '22 13:09 SourceCipher

this issue happens on a clean installation of CRA 5.0.1 whether trying to use process inside the component or not. None of the proposed solutions worked for me including using react error overlay 6.0.9 or yarn

enoguchi-lmi avatar Sep 24 '22 06:09 enoguchi-lmi

tried everything else, but still stuck with this exact issue

abhi-dasgupta avatar Sep 27 '22 17:09 abhi-dasgupta

No way to solve this. It's totaly frustrating. How are we supposed to start another project with CRA if there are those incredible issues?

Veeenz avatar Sep 29 '22 07:09 Veeenz

No way to solve this. It's totaly frustrating. How are we supposed to start another project with CRA if there are those incredible issues?

If your project or library doesnt follow this practice then you will encounter this. Please loo at this documentation https://create-react-app.dev/docs/adding-custom-environment-variables/

masterbater avatar Sep 30 '22 03:09 masterbater

From last 2 days tried everything which is avail in internet, but still stuck with the same issue

arpit-ma avatar Oct 03 '22 11:10 arpit-ma

How did you solve it a day later I have the same issue.

zithokamvalethu avatar Oct 04 '22 07:10 zithokamvalethu

Stupid error,still stuck with this exact issue.

HannanHan avatar Oct 05 '22 12:10 HannanHan

I have used all ( with "react-scripts": "^5.0.1").
"resolutions": { "react-error-overlay": "^6.0.11" },

"scripts":{ "preinstall": "npx npm-force-resolutions", .... },

"devDependencies":{ "react-error-overlay": "^6.0.11", ... }

Not working. I think react-scripts v4 using is not a solution. Now using
const { env } = require('process'); env.NODE_ENV to solve the problem.

sourcecode71 avatar Oct 05 '22 13:10 sourcecode71

If you possibly use process?.env?.etc remove the question marks so process.env.etc

iweurman avatar Oct 18 '22 17:10 iweurman

For people still stuck with that error, here is how I fixed it. I used craco because did not want to eject from RS. Then in the craco.config.js having this:

const webpack = require('webpack');

module.exports = {
    webpack: {
        plugins: {
            add: [
                new webpack.ProvidePlugin({
                    process: 'process/browser.js',
                })
            ]
        },
        configure: {
            resolve: {
                fallback: {
                    'fs': false,
                    'path': false,
                    'crypto': false,
                }
            },
        },
    },
};

You might not need to have the configure.resolve.fallback part, but if you're stuck with error like "fs/path/crypto" is not defined, you have the resolution here too :)

You'll also need to have process as dev dependency

yarn add -D process

dbuchet avatar Dec 08 '22 13:12 dbuchet

any update on this from the CRA team? | feels like not so many people use the process therefore this is still an open issue?

joelgrimberg avatar Dec 24 '22 17:12 joelgrimberg

I'm facing this issue using Vite + React

Decryptus007 avatar Jan 03 '23 22:01 Decryptus007

I'm facing this issue using Vite + React

Vite uses different syntax

  • import.meta.env.VITE_API_URL instead of process.env.

Ronald-patrick avatar Jan 12 '23 16:01 Ronald-patrick

I am facing this issue with react-app-rewired, when anything is updated in the source code then page buttons or any clickable thing becomes unresponsive, and we need to reload the page again and again.

Uncaught ReferenceError: process is not defined at 4043 (:2:13168) at r (:2:306599) at 8048 (:2:9496) at r (:2:306599) at 8641 (:2:1379) at r (:2:306599) at :2:315627 at :2:324225 at :2:324229 at e.onload (index.js:1:1)

mdtausifiqbal avatar Mar 08 '23 07:03 mdtausifiqbal

I am facing this issue with react-scripts-rewired, when anything is updated in the source code then page buttons or any clickable thing becomes unresponsive, and we need to reload the page again and again.

Uncaught ReferenceError: process is not defined at 4043 (:2:13168) at r (:2:306599) at 8048 (:2:9496) at r (:2:306599) at 8641 (:2:1379) at r (:2:306599) at :2:315627 at :2:324225 at :2:324229 at e.onload (index.js:1:1)

You can use the below to package.json

//if you use npm "devDependencies": { "react-error-overlay": "6.0.9" }, "overrides": { "react-error-overlay": "6.0.9" }

//if you use yarn "devDependencies": { "react-error-overlay": "6.0.9" }, "resolutions": { "react-error-overlay": "6.0.9" }

and try reinstalling deps, this might be because of react-scripts >=4 have this react-error-overlay dependency which you might not be installed in your project

niranjan404 avatar Mar 08 '23 07:03 niranjan404

I am facing this issue with react-scripts-rewired, when anything is updated in the source code then page buttons or any clickable thing becomes unresponsive, and we need to reload the page again and again. Uncaught ReferenceError: process is not defined at 4043 (:2:13168) at r (:2:306599) at 8048 (:2:9496) at r (:2:306599) at 8641 (:2:1379) at r (:2:306599) at :2:315627 at :2:324225 at :2:324229 at e.onload (index.js:1:1)

You can use the below to package.json

//if you use npm "devDependencies": { "react-error-overlay": "6.0.9" }, "overrides": { "react-error-overlay": "6.0.9" }

//if you use yarn "devDependencies": { "react-error-overlay": "6.0.9" }, "resolutions": { "react-error-overlay": "6.0.9" }

and try reinstalling deps, this might be because of react-scripts >=4 have this react-error-overlay dependency which you might not be installed in your project

it Solved my problem, thanks

mdtausifiqbal avatar Mar 08 '23 07:03 mdtausifiqbal

For me guys it's working in one hit

 "overrides": {
    "react-error-overlay": "6.0.9"
  }

Please try this one

Shivamrbic avatar May 23 '23 07:05 Shivamrbic

Tenía el mismo problema en React + Vite, pero esto me solucionó:

Vite usa una sintaxis diferente

  • import.meta.env.VITE_API_URL en lugar de process.env.

Harmed2002 avatar Jun 14 '23 22:06 Harmed2002

Tried all the above approaches but no luck.

SainagChunduru avatar Jun 29 '23 12:06 SainagChunduru

If it was an error with simple peers, this solved it for me: https://stackoverflow.com/questions/74421952/uncaught-referenceerror-process-not-defined-simple-peer-javascript

Samrj12 avatar Aug 15 '23 01:08 Samrj12

From stackoverflow this "works" For some reason plain process is undefined but process.NODE_ENV (etc) works. This is baffling.


# app.js
function App() {
    useEffect(() => {
      window.process = {
        ...window.process,
      };
    }, []);
   return (
   <div className="App">
      THIS IS OKAY
      {process.env.REACT_APP_API_KEY)}
      THIS IS NOT - it's undefined
      {process) 
   <Todo />
   </div>
  );
}

chrisdel101 avatar Sep 07 '23 21:09 chrisdel101

Tenía el mismo problema en React + Vite, pero esto me solucionó:

Vite usa una sintaxis diferente

  • import.meta.env.VITE_API_URL en lugar de process.env.

Hi @Harmed2002 , process.env is generating on vite build command. it is being generated in __federation_shared_react.js file with below condition process.env.NODE_ENV === 'production'

can you please give any more details to resolve this issue ?

prasanthyejje avatar Jan 23 '24 14:01 prasanthyejje