"jsx"

Specifies what the compiler does with JSX syntax in .tsx files.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "module": "ESNext",
    "moduleResolution": "Bundler"
  },
  "include": ["src"]
}

Definition and usage

#

The jsx option tells the compiler how to process JSX markup in .tsx files. Without it, every such tag fails with the error “Cannot use JSX unless the '--jsx' flag is provided”.

With the value react-jsx, JSX is turned into function calls from the react/jsx-runtime module, whose import the compiler adds by itself. This is the recommended setting since React 17: you don’t need to import React in files with JSX. preserve leaves the JSX unchanged for a tool that will process it later, and the older react turns it into React.createElement().

The jsxImportSource option lets you use the same syntax with another library, such as Preact. To type-check JSX, the compiler needs the library’s types, which for React means the @types/react package.

Syntax

#
Syntax
"jsx": "react-jsx"

Available values

#
  • react-jsx

    _jsx("span", …)

    Calls from react/jsx-runtime, with the import added automatically. Recommended for React 17 and newer.
  • react-jsxdev

    _jsxDEV("span", …)

    Like react-jsx, but with debugging information: the file, line, and column of the tag.
  • preserve

    <span>…</span>

    JSX is left unchanged, and the output file has the .jsx extension. For projects where a bundler or framework processes the JSX.
  • react

    React.createElement("span", …)

    The older way. React must be available in every file with JSX.
  • react-native

    <span>…</span>

    JSX is left unchanged, but the output file has the .js extension.

More examples

#
What react-jsx generates
TypeScript
// src/Badge.tsx
export function Badge({ label }: { label: string }) {
  return <span className="badge">{label}</span>
}

// Compilation result with "jsx": "react-jsx":
// import { jsx as _jsx } from "react/jsx-runtime";
// export function Badge({ label }) {
//     return _jsx("span", { className: "badge", children: label });
// }
The same syntax with Preact
JSON
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}

Good practices

#
  • In a Next.js or Vite project, keep the value from the template. The tool sets jsx the way its compiler needs it.
  • The error “This JSX tag requires the module path 'react/jsx-runtime' to exist” means the React types are missing. Install the @types/react package.
  • Files with JSX must have the .tsx extension. In a .ts file, the tag is read as a type assertion or a comparison, and you get misleading syntax errors.

Related entries

#

Spotted a mistake or missing an example? Write to us.