How to Fix Can't Resolve 'reactjsx-runtime' Error in React

This guide provides concise solutions to resolve the “Can’t resolve ‘react/jsx-runtime’” error in React, commonly encountered when upgrading to React 17 or later.
Why This Error Occurs
React 17 introduced a new JSX runtime (react/jsx-runtime
) to automate JSX transformation. If your project isn’t configured to use this runtime, the compiler fails to locate the required module, resulting in the error.
Benefits of the New JSX Runtime
- No Need to Import React: JSX can be used without explicitly importing React.
- Smaller Bundle Size: The new runtime can reduce JavaScript bundle size.
Solutions to Fix the Error
1. Upgrade React and React-DOM to Version 17+
Ensure your project uses React 17 or higher. Update package.json
:
1 | "dependencies": { |
Run npm install
or yarn install
to apply changes.
2. Configure tsconfig.json
for TypeScript Projects
Update tsconfig.json
to use the new JSX runtime:
1 | { |
3. Modify Webpack Configuration
For webpack projects, resolve the JSX runtime paths in webpack.config.js
:
1 | module.exports = { |
4. Upgrade to React 18 (Optional)
React 18 includes internal webpack configurations to resolve this issue. Update package.json
:
1 | "dependencies": { |
Note: Ensure compatibility with other dependencies before upgrading.
Additional Tips
- Check Dependency Conflicts: Ensure all dependencies support React 17+.
- Use Create React App (CRA): Update CRA to a version supporting the new JSX runtime.
- Clear Cache: Run
npm cache clean --force
to resolve lingering issues.
Conclusion
Resolve the “Can’t resolve ‘react/jsx-runtime’” error by upgrading React, configuring tsconfig.json
, or modifying webpack settings. These steps ensure a smooth transition to the new JSX runtime, improving performance and maintainability.
For more details, refer to the official React documentation.