乐闻世界logo
搜索文章和话题

Cannot use JSX unless the '-- jsx ' flag is provided

1个答案

1

Regarding the issue you raised about 'React error showing JSX not configured', this error typically occurs when developing React applications with TypeScript. This is because TypeScript requires explicit instructions to parse JSX syntax. The error message typically indicates that the TypeScript configuration file (i.e., tsconfig.json) lacks the correct settings to support JSX.

  1. Check and update the tsconfig.json file: Ensure the tsconfig.json file has the following configuration:
json
{ "compilerOptions": { "jsx": "react" } }

Here, "jsx": "react" instructs TypeScript to use React's JSX transformation. If using the new JSX transformation in React 17 or higher, set it to "jsx": "react-jsx".

  1. Restart the development server: After modifying the configuration, restart your development server (e.g., using npm start with Create React App) to apply the changes.

  2. Check TypeScript version: Ensure the TypeScript version you are using supports your JSX configuration. For example, if you use "jsx": "react-jsx", you need TypeScript 4.1 or higher.

For example, in a previous project, we encountered a similar issue when migrating from Create React App's default settings to a custom Webpack configuration. Initially, we overlooked the jsx configuration in tsconfig.json. After identifying and fixing this, the project compiled correctly, and the error disappeared.

2024年6月29日 12:07 回复

你的答案