question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nx react app gets 'global is not defined'; create-react-app version doesn't

See original GitHub issue

Current Behavior

Uncaught ReferenceError: global is not defined in Chrome when loading application.

Expected Behavior

Page displays syntax colored JSON.

Steps to Reproduce

See https://github.com/dkorz/nx-react-syntax-highlighter to reproduce problem. See https://github.com/dkorz/react-highlighter version that works.

Breakpointing in the create-react-app version shows global defined although it appears not to be window. I can make the nx version work by adding the following lines to polyfill.ts but that doesn’t feel right.

if (!('global' in window)) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  (window as any).global = window;
}

Note that I got this component from https://github.com/auth0-blog/auth0-react-sample.git that I was converting to nx.

Failure Logs

core.js:221 Uncaught ReferenceError: global is not defined
    at capture (core.js:221)
    at Object.../../../node_modules/refractor/core.js (core.js:5)
    at __webpack_require__ (bootstrap:79)
    at Module.../../../node_modules/react-syntax-highlighter/dist/esm/prism-light.js (prism-light.js:1)
    at __webpack_require__ (bootstrap:79)
    at Module.../../../node_modules/react-syntax-highlighter/dist/esm/index.js (index.js:1)
    at __webpack_require__ (bootstrap:79)
    at Module.../../../libs/ui/src/lib/highlight/highlight.tsx (highlight.tsx:1)
    at __webpack_require__ (bootstrap:79)
    at Module.../../../libs/ui/src/index.ts (index.ts:1)

Environment

nx : Not Found @nrwl/angular : Not Found @nrwl/cli : 10.1.0 @nrwl/cypress : 10.1.0 @nrwl/eslint-plugin-nx : 10.1.0 @nrwl/express : Not Found @nrwl/jest : 10.1.0 @nrwl/linter : 10.1.0 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/react : 10.1.0 @nrwl/schematics : Not Found @nrwl/tao : 10.1.0 @nrwl/web : 10.1.0 @nrwl/workspace : 10.1.0 typescript : 3.9.7

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

30reactions
m7mdBalboulcommented, Nov 3, 2020

This should fix it,

add webpack.config.js to your app with the following

const nrwlConfig = require("@nrwl/react/plugins/webpack.js"); // require the main @nrwl/react/plugins/webpack configuration function.

module.exports = (config, context) => {
    
  nrwlConfig(config); // first call it so that it @nrwl/react plugin adds its configs, 
 
// then override your config.
  return {
    ...config,
    node: { global: true, fs: 'empty' }, // Fix: "Uncaught ReferenceError: global is not defined", and "Can't resolve 'fs'".
  };
};

then in workspaces.json change webpackConfig in options to "webpackConfig": "apps/your-app/webpack.config.js"

0reactions
eldrigecommented, Nov 2, 2022

Here is a work around I found, via stackoverflow. You can this options to your polyfills.ts file for your specific app. `(window as any).global = window;

(window as any).process = { env: { DEBUG: undefined }, }; `

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactstrap typescript nx `Uncaught ReferenceError: global is ...
I'm using reactstrap@8.6.0 with nx and typescript. But if any component is included from reactstrap the app is crashing with below error
Read more >
react global is not defined | The AI Search Engine You Control
Breakpointing in the create-react-app version shows global defined although it appears not to be window . I can make the nx version work...
Read more >
Create a New React App
Create React App doesn't handle backend logic or databases; it just creates a frontend build pipeline, so you can use it with any...
Read more >
The React CLI you always wanted but didn't know about
You should end up with a new Nx workspace and our happynrwl React app in the apps/ folder ... you do not only...
Read more >
React ReferenceError: process is not defined | bobbyhadz
To solve the "Uncaught ReferenceError: process is not defined" in React, open your terminal in your project's root directory and update the version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found