React JSX fails because React is undefined when it calls createElement
Cannot read property 'createElement' of undefinedShort answer
The classic JSX transform turns every tag into React.createElement, so the file breaks when the React name is undefined. A default import of React through TypeScript without esModuleInterop is the usual reason. Import React as a namespace, turn on esModuleInterop, or use the automatic JSX runtime.
Fix it fast
1Find out which cause you have
Print what the import gives you. If it prints undefined, the import form does not match how the module exports.
// at the top of the failing file import React from 'react'; console.log(typeof React);2Match what you see, then jump to its fix
- 1
TypeScript or a TS-compiled project, import React from 'react', and typeof React prints undefined.A default import of a CommonJS module without esModuleInteropFix - 2
The compiled output contains React.createElement calls, and a file forgot the import or gets it wrong.The project relies on the classic transformFix
- 1
What does the error look like?
Cannot read property 'createElement' of undefined
Cause 1 of 2
A default import of a CommonJS module without esModuleInterop
1Check
The TypeScript docs say that without esModuleInterop a default import compiles to require(...).default, and most CommonJS libraries do not set a default property. Check the flag.
grep -n esModuleInterop tsconfig.json2Fix
Turn on esModuleInterop, or import the module as a namespace.
// tsconfig.json
{ "compilerOptions": { "esModuleInterop": true } }
// or in the file
import * as React from 'react';3Verify
typeof React now prints object, and the file renders.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 2
The project relies on the classic transform
1Check
The React team's new JSX transform imports its helpers from react/jsx-runtime, so tags no longer need the React name in scope. It needs Babel 7.9 or newer, and React 17 or the backported 16.14, 15.7 or 0.14.10.
npm ls @babel/core
grep -rn "runtime" babel.config.json .babelrc 2>/dev/null2Fix
Switch Babel's React preset to the automatic runtime.
{
"presets": [
["@babel/preset-react", { "runtime": "automatic" }]
]
}3Verify
Build again. Compiled files import from react/jsx-runtime, not React.createElement.
npm run buildConfirmed by legacy.reactjs.org
How do I stop it coming back?
- Use the automatic JSX runtime on every new project.
- Keep one TypeScript setting for module interop across the repo.
- Still import hooks by name from react, since the new transform only removes the need for React in JSX.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Back end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fixBack end
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fix