Babel or Jest stops on JSX with an 'experimental syntax' error
SyntaxError: Support for the experimental syntax 'jsx' isn't currently enabledShort 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
The 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
The 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
The syntax error is gone but tests now fail with 'React is not defined'.You chose the classic runtime but React is not in scopeFix
- 1
What does the error look like?
SyntaxError: Support for the experimental syntax 'jsx' isn't currently enabledAdd @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.js2Fix
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.jsxConfirmed by babeljs.io
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.js2Fix
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 jestConfirmed by jestjs.io
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.js2Fix
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 jestConfirmed by babeljs.io
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:
- 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.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix