Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

React JSX fails because React is undefined when it calls createElement

ErrorCannot read property 'createElement' of undefined

Short 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.

  • Applies to React 18 and 19, TypeScript 5, Babel 7.9 or newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1TypeScript or a TS-compiled project, import React from 'react', and typeof React prints undefined.A default import of a CommonJS module without esModuleInteropFix
    2. 2The compiled output contains React.createElement calls, and a file forgot the import or gets it wrong.The project relies on the classic transformFix

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.json

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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/null

2Fix

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 build

Confirmed by legacy.reactjs.org

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.