Bobcares

Quick Steps to Fix Error “‘jsx’ is not exported by node_modules/react/jsx-runtime.js”

by | Aug 7, 2024

The error message ‘jsx’ is not exported by node_modules/react/jsx-runtime.js typically occurs when we’re trying to use a newer JSX transformation with an older version of React or when there is a misconfiguration in the build setup. Here, we’ll look into the causes and fixes for the issue. As part of our Server Management Service, we assist our customers with several queries.

Overview
  1. Fixing error “‘jsx’ is not exported by node_modules/react/jsx-runtime”
  2. Causes & Solutions of the Error
  3. Key Considerations
  4. Conclusion

Fixing error “‘jsx’ is not exported by node_modules/react/jsx-runtime”

React applications often face the issue “Error: ‘jsx’ is not exported by node_modules/react/jsx-runtime.js” when they are unable to discover the JSX runtime files needed to translate JSX code. This error suggests that there is a dependency or configuration issue with the project. This error has a number of frequent causes. Let’s see the details in the following sections.

error 'jsx' is not exported by node_modules/react/jsx-runtime.js

Causes & Solutions of the Error

1. Outdated React Version: React 17 introduced a new JSX transform that allows users to use JSX without explicitly importing React in every file. If the project uses an older React version, this feature may not be supported.

Solution:

We must update React to version 17 or later using the following command:

npm install react@latest react-dom@latest

2. Misconfigurations in Project Settings: Tools like Webpack or Rollup need proper configurations in order to process JSX correctly.

Solution:

Make sure the configuration files include rules for handling JSX. For Webpack, use the following:

javascript

module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
},
},
},
],
}

3. Version Mismatches Between Dependencies: Conflicting versions of React, ReactDOM, and related libraries can cause JSX issues.

Solution:

We must check as well as update the dependencies in package.json:

bash

npm outdated

Lastly, update any mismatched dependencies to compatible versions.

4. Incorrect Import Statements

Solution:

For React versions prior to 17, we must import React at the beginning of the JSX files using the following code:

javascript

import React from 'react';

5. Module Conflicts Within the React App: Multiple versions of React or conflicting node_modules can lead to issues.

Solution:

So, delete node_modules and package-lock.json, then reinstall dependencies:

bash

rm -rf node_modules package-lock.json
npm install

6. Additional Steps for Rollup Users: Rollup users may need extra configuration for the new JSX transform.

Solution:

Add react/jsx-runtime to the external option in rollup.config.js:

javascript

export default {
// ...
external: ['react/jsx-runtime'],
// ...
}


Key Considerations

When dealing with the error ‘jsx’ is not exported by node_modules/react/jsx-runtime.js or setting up a React project using the new JSX transform, there are several key considerations to keep in mind in order to ensure smooth development and compatibility. Following are the important points to consider:

1. React Version Compatibility

i. Ensure React 17 or Later: The new JSX transform is supported starting from React 17. So, using an older version can lead to issues. So, we must update React and ReactDOM to the latest version with the following code:

bash
npm install react@latest react-dom@latest

ii. Check Related Libraries: Ensure all related libraries (e.g., react-scripts, @types/react) are compatible with React 17 or later.

2. Build Tool Configuration

i. Babel Configuration: Use @babel/preset-react with the automatic runtime option in order to enable the new JSX transform.

Babel Configuration:

json

{
"presets": [
"@babel/preset-env",
["@babel/preset-react", { "runtime": "automatic" }]
]
}

ii. Webpack Configuration: Ensure babel-loader is configured to process JSX files.

Webpack Rule Example:

javascript

module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
},
},
},
],
}

iii. Rollup Configuration: Add react/jsx-runtime to the external option if using Rollup.

Rollup Configuration:

javascript

export default {
// ...
external: ['react/jsx-runtime'],
// ...
}

3. Import Statements

i. New JSX Transform: In React 17+, we don’t need to import React in every file using JSX.

ii. Older React Versions: If using a version prior to React 17, we must also ensure we import React:

javascript

import React from 'react';

4. Dependency Management

i. Check for Version Conflicts: Also, ensure there are no mismatched versions of React or related libraries. Use npm outdated as in the following code in order to find and resolve version conflicts.

bash

npm outdated

ii. Clear Cache and Reinstall: Sometimes clearing the node_modules cache also helps resolve conflicts.

bash

rm -rf node_modules package-lock.json
npm install

5. Module System Consistency: Ensure the project consistently uses either ES Modules or CommonJS in order to avoid import/export mismatches.

6. Testing and Development Environment

i. Browser Compatibility: Ensure the project is tested in environments that support ES6+ as well as the new JSX transform.

ii. Development Tools: We can also use tools like ESLint and Prettier configured for React 17+ in order to ensure code quality as well as consistency.

7. Community and Documentation

i. React Documentation: Regularly check React’s official documentation for updates and best practices.

ii. Community Resources: Engage with the React community on platforms like Stack Overflow or Reddit for support as well as insights.

By considering these key points, we can ensure a smooth setup and operation of the React project, avoiding common pitfalls related to JSX processing and library compatibility.

[Searching solution for a different question? We’re happy to help.]

Conclusion

In conclusion, updating React and its dependencies to the most recent versions is essential to fixing the problem and ensuring a seamless React development experience. Make sure we have included the appropriate rules in the Webpack or Rollup setup so that it handles JSX correctly. Also, make sure the import statements are accurate, particularly if we’re using several React versions.

We can also try removing the package-lock.json and node_modules files and reinstalling dependencies to see if it resolves any lingering problems. For Rollup users, it’s important to configure the external option correctly to support the new JSX transform. Following these steps from our Techs will help us maintain a stable and efficient React environment.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF