create-react-app: Uncaught ReferenceError: process is not defined (NOT react-error-overlay)
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
- Install CRA
yarn create react-app temp-app --template typescript
- 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
Reproducible demo
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 13
- Comments: 30 (1 by maintainers)
this is so stupid… can not believe react team could make such mistake…
Vite uses different syntax
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 thecraco.config.js
having this: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 dependencyI 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
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.I’m facing this issue using Vite + React
any update on this from the CRA team? | feels like not so many people use the process therefore this is still an open issue?
If you possibly use process?.env?.etc remove the question marks so process.env.etc
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
I am getting this error by simply using destructuring in an export.
We are trying to update some libraries and we are getting this issue. Tried to add this
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?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
Tried all the above approaches but no luck.
For me guys it’s working in one hit
Please try this one
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.
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?
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.From stackoverflow this “works” For some reason plain
process
is undefined butprocess.NODE_ENV
(etc) works. This is baffling.