Fixing React’s “Cannot Read Property createElement of Undefined”
Fixing React’s “Cannot Read Property createElement of Undefined”
The error “Cannot read property ‘createElement’ of undefined” in React appears when the environment attempts to call React.createElement but React itself is undefined. Because JSX compiles into calls to createElement, React must be present and properly resolved. When it is not, rendering fails immediately.
This guide explains the most common reasons this happens, how JSX compilation works behind the scenes, and how to fix the issue in modern React environments using bundlers like Vite, Webpack, Next.js, and others.
1. Why the Error Happens: JSX Compilation
Older JSX transforms (React < 17) compile:
<div>Hello</div>into:
React.createElement("div", null, "Hello");If React is undefined, createElement is undefined as well.
React 17 and React 18 introduced the “automatic JSX transform,” which doesn’t require a manual import React …, but bundlers must still correctly resolve the React package.
If React fails to load, JSX cannot compile, which leads to this error.
2. Ensure React and ReactDOM Are Properly Imported
React 17 and earlier:
import React from "react";
import ReactDOM from "react-dom";React 18:
import React from "react";
import { createRoot } from "react-dom/client";Accidental import mistakes, typos, or missing imports cause React to be undefined during compilation.
3. Verify React Installation
Run:
npm install react react-domIf either package is missing, partial, or corrupted, bundlers will pass undefined into your compiled JSX code.
4. Fix React Version Mismatches
React and ReactDOM must use compatible versions.
Check:
"dependencies": {
"react": "18.x",
"react-dom": "18.x"
}Fix mismatches:
npm install react@latest react-dom@latestMismatches are one of the most frequent causes when upgrading React.
5. If Using React.createElement Manually
Some setups use React without JSX:
const node = React.createElement("div", null, "Hello");If this fails, React is undefined or incorrectly imported:
console.log(React);If this logs {} or undefined, bundler resolution is broken.
6. CDN Usage and Script Ordering Problems
When using CDN:
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>React must load:
- before ReactDOM
- before your application scripts
Mixing module scripts with UMD builds also breaks React imports.
7. Bundler Configuration Issues (Vite, Webpack, Rollup)
Common problems include:
- incorrect alias configuration
- broken
node_modulesresolution - incompatible ESM/CJS hybrid dependencies
- stale build caches
- incorrect JSX runtime settings
Clear caches:
rm -rf node_modules
rm -rf dist .next .vite
npm install
npm run buildJSX runtime mismatch (React 17+)
If your tsconfig.json contains:
"jsx": "react-jsx"your bundler must support the automatic JSX runtime. Wrong config = undefined React.
8. Using the Correct Rendering API for Your Version
React 18:
const root = createRoot(document.getElementById("root"));
root.render(<App />);React < 18:
ReactDOM.render(<App />, document.getElementById("root"));Combining old ReactDOM with createRoot results in undefined behavior and often this exact error.
9. Debugging Checklist
Check React import:
console.log("React:", React);Check installed versions:
npm ls react react-domReinstall dependencies:
rm -rf node_modules
npm installConfirm no conflicting packages exist:
-
preactaliases replacingreact - mixed CJS/ESM builds
- config rewriting
reactimports
Conclusion
“Cannot Read Property createElement of Undefined” signals that React failed to load or resolve, and JSX compilation is calling methods on an undefined value. Fixing it requires:
- matching React + ReactDOM versions,
- correct imports,
- proper bundler configuration,
- and using the right rendering API for your React version.
Once React is properly installed and your tools know how to resolve it, the error disappears and JSX works as expected.