Fixing React’s “Cannot Read Property createElement of Undefined”

January, 7th 2025 3 min read

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:

jsx
<div>Hello</div>

into:

js
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:

jsx
import React from "react";
import ReactDOM from "react-dom";

React 18:

jsx
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:

bash
npm install react react-dom

If 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:

json
"dependencies": {
  "react": "18.x",
  "react-dom": "18.x"
}

Fix mismatches:

bash
npm install react@latest react-dom@latest

Mismatches are one of the most frequent causes when upgrading React.


5. If Using React.createElement Manually

Some setups use React without JSX:

js
const node = React.createElement("div", null, "Hello");

If this fails, React is undefined or incorrectly imported:

js
console.log(React);

If this logs {} or undefined, bundler resolution is broken.


6. CDN Usage and Script Ordering Problems

When using CDN:

html
<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:

  1. before ReactDOM
  2. 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_modules resolution
  • incompatible ESM/CJS hybrid dependencies
  • stale build caches
  • incorrect JSX runtime settings

Clear caches:

bash
rm -rf node_modules
rm -rf dist .next .vite
npm install
npm run build

JSX runtime mismatch (React 17+)

If your tsconfig.json contains:

json
"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:

jsx
const root = createRoot(document.getElementById("root"));
root.render(<App />);

React < 18:

jsx
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:

js
console.log("React:", React);

Check installed versions:

bash
npm ls react react-dom

Reinstall dependencies:

bash
rm -rf node_modules
npm install

Confirm no conflicting packages exist:

  • preact aliases replacing react
  • mixed CJS/ESM builds
  • config rewriting react imports

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.