Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Babel or Jest stops on JSX with an 'experimental syntax' error

ErrorSyntaxError: Support for the experimental syntax 'jsx' isn't currently enabled

Short answer

Babel only parses JSX when a React preset is active. The error means the Babel config that Jest loaded has no @babel/preset-react, or Jest never found a config at all. Install the preset, list it in babel.config.js at the project root, and make sure Jest is not told to skip Babel.

  • Applies to Node.js 22, Babel 7, Jest 29, @babel/preset-react 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the Babel config that is actually in the project root. If no file shows up, or it has no react preset, that is the cause. Then look at the transform setting in your Jest config.

    ls babel.config.* .babelrc* 2>/dev/null
    grep -n "transform" jest.config.* package.json 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on the first line that contains a JSX tag, and your babel.config.js lists only @babel/preset-env.The React preset is not installed or not listedFix
    2. 2The preset is installed and listed, yet the same error remains in Jest only.Jest is told not to use Babel, or never finds the configFix
    3. 3The syntax error is gone but tests now fail with 'React is not defined'.You chose the classic runtime but React is not in scopeFix

What does the error look like?

  • SyntaxError: Support for the experimental syntax 'jsx' isn't currently enabled
  • Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.

Cause 1 of 3

The React preset is not installed or not listed

1Check

A Jest setup that follows the Jest docs usually has only @babel/preset-env. That preset does not understand JSX.

cat babel.config.js

2Fix

Install the React preset as a dev dependency and add it to the presets array. With the default automatic runtime you do not need to import React in every file.

npm install --save-dev @babel/preset-react

// babel.config.js
module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }],
    ['@babel/preset-react', { runtime: 'automatic' }],
  ],
};

3Verify

Run the test file that failed.

npx jest path/to/failing.test.jsx

Confirmed by babeljs.io

Did this fix it?

Cause 2 of 3

Jest is told not to use Babel, or never finds the config

1Check

babel-jest ships with Jest and transforms files whenever a Babel config exists. Setting transform to an empty object turns that off, and a config outside the project root may not be picked up.

grep -n -A2 "transform" jest.config.js
ls babel.config.js

2Fix

Remove the empty transform override so babel-jest runs again, and keep babel.config.js in the project root next to package.json.

// jest.config.js : delete this line
// transform: {},

3Verify

Rerun Jest. A run with no JSX error and real test results means Babel is applied.

npx jest

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

You chose the classic runtime but React is not in scope

1Check

With runtime set to classic, Babel imports nothing for you and compiles JSX to React.createElement calls. The automatic runtime is the default since Babel 8, while Babel 7 needs it set explicitly.

grep -n "runtime" babel.config.js

2Fix

Set runtime to automatic, or import React at the top of every file that has JSX.

['@babel/preset-react', { runtime: 'automatic' }]

3Verify

Run the tests again.

npx jest

Confirmed by babeljs.io

Did this fix it?

How do I stop it coming back?

  • Keep one Babel config file at the project root and commit it with the project.
  • When you add a new file type to a project, run its tests once straight away.
  • Prefer the automatic runtime so test files need no React import.

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

Next step

Want this handled for good?

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