Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Every time I run npm start, it overrides whatever I configure in {jsx: ...} with react-jsx in order to be compatible with JSX transform in React 17.

The following changes are being made to your tsconfig.json file:
  - compilerOptions.jsx must be react-jsx (to support the new JSX transform in React 17)

The problem is VSCode using an older version of typescript (4.0.3), while the typescript version shipped with the project is (4.1.2).

The following did the trick for me:

  1. Go to the command palette CTRL+Shift+P (Or +Shift+P on Mac).
  2. Choose "TypeScript: Select a TypeScript Version...".
  3. Choose "Use workspace Version".

VSCode status bar

VSCode command palette

TypeScript workspace version


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

Restart your IDE. Sometimes tsconfig.json changes aren't immediately picked up 🌹


For whoever reading, go to the tsconfig.json and change that line from react-jsx to react:

{
  "compilerOptions": {
    "jsx": "react"
  }
}

bonus: try also setting the IDE TS version to the latest (atm 4.2), in vscode CMD + SHIFT + P and change it from there.


create-react-app is broken out of the box. AS OF 2021 DECEMBER

Inside of tsconfig, include was set only to /src, Change to:

  "include": [
    "./src/**/*.ts"
  ]

In my case, the issue was that VSCode generated an empty tsconfig.json file which, of course, did nothing.

I added this to tsconfig.json from Typescript's React Page:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react"
    }
}

...then hit save and VSCode took it from there.


If you are seeing this issue after running create-react-app with Typescript You can solve this problem by adding "typescript.tsdk": "node_modules/typescript/lib" to .vscode/settings.json.

For IntelliSense, if you use "jsx": "react-jsx" you need to switch your workspace to use TS 4.1+

Or visually, simply go down to the blue task bar and select the Typescript version (Probably 4.x.x something), then select "Select TypeScript Version".

enter image description here

Then select "Use Workspace Version version" which should reference node_modules/typescript/lib

enter image description here