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.

"React is not defined" when using a custom _app.tsx and .babelrc using React 17

See original GitHub issue

Bug report

After upgrading to React 17, I can no longer compile my project with a custom .babelrc file without importing React in every *.tsx file. Having a _app.tsx file seems to be required to reproduce the issue. The error returned is:

Error occurred prerendering page "/". Read more: https://err.sh/next.js/prerender-error
ReferenceError: React is not defined
    at MyApp.render (/home/bruhno/projects/react-not-defined/.next/server/pages/_app.js:297:70)
    at d (/home/bruhno/projects/react-not-defined/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:35:231)
    at bb (/home/bruhno/projects/react-not-defined/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:16)
    at a.b.render (/home/bruhno/projects/react-not-defined/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:42:43)
    at a.b.read (/home/bruhno/projects/react-not-defined/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:41:83)
    at exports.renderToString (/home/bruhno/projects/react-not-defined/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:52:138)
    at Object.renderPage (/home/bruhno/projects/react-not-defined/node_modules/next/dist/next-server/server/render.js:50:851)
    at Function.getInitialProps (/home/bruhno/projects/react-not-defined/.next/server/pages/_document.js:273:19)
    at loadGetInitialProps (/home/bruhno/projects/react-not-defined/node_modules/next/dist/next-server/lib/utils.js:5:101)
    at renderToHTML (/home/bruhno/projects/react-not-defined/node_modules/next/dist/next-server/server/render.js:50:1142)

I’m not sure if TypeScript is related to the issue, but I’ve simply copy pasted minimal configuration from my own project which is where I experienced the issue.

To Reproduce

I’ve created the following repo to reproduce the error: https://github.com/NBruhno/nextjs-react-not-defined

  1. run yarn
  2. run yarn dev or yarn build
  3. See error in the terminal and on the rendered page

Expected behavior

I could before React 17 run my repo without having to import React in my .tsx files unless explicitly referring to some functions, with my own custom .babelrc file.

System information

  • OS: Windows running WSL: Ubuntu-20.04
  • Version of Next.js: 9.5.5
  • Version of Node.js: v12.17.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:29
  • Comments:37 (16 by maintainers)

github_iconTop GitHub Comments

13reactions
vasco3commented, Oct 21, 2020

I’m getting a similar error but i’m not using TS

8reactions
PabloSzxcommented, Oct 27, 2020

A workaround is to modify your .babelrc adding "@babel/plugin-transform-react-jsx" plugin manually, you don’t need to add it to your package.json, since Next.js itself has it as a dependency

{
  "presets": ["next/babel"],
  "plugins": [
    [
      "@babel/plugin-transform-react-jsx",
      {
        "runtime": "automatic"
      }
    ]
  ]
}

It doesn’t seem to work all the time, and _document.tsx has problems too

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught ReferenceError: React is not defined - Stack Overflow
The solution is to either remove this piece of configuration (so React will be bundled with your javascript) or load the React framework ......
Read more >
React is not defined after migrating to React 17 - Meteor forums
Hello,. I migrated to React v17. ... Tried creating a babel.config.js file with the following code but is not working. module.exports = {...
Read more >
Introducing the New JSX Transform – React Blog
Although React 17 doesn't contain new features, it will provide support for a new version of the JSX transform. In this post, we...
Read more >
Typescript + Parcel new JSX transform - React is not defined ...
I've identified the problem is the version of a few babel plugins that are outdated that Parcel (version 2.0.0-beta.3.1 at time of writing)...
Read more >
babel/preset-react
Decides which runtime to use. automatic auto imports the functions that JSX transpiles to. classic does not automatic import anything. development.
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