typescript ts-node无法编译JSX标记

quhf5bfb  于 2022-12-05  发布在  TypeScript
关注(0)|答案(1)|浏览(216)

我试图将JSX标记编译成HTML,然后将它们刷新成HTML文件,但不知何故,ts-node无法编译以下代码。

private index = (request: Request, response: Response) => {
    // const component = React.createElement(Index, {key: 1, name: "Aman Sharma"}) // this works
    const component = <Index key={1} name="Aman Sharma" /> // throws error
    const html = ReactDOMServer.renderToString(component)
    fs.writeFileSync('website/index.html', html);
    response.status(200).send();
}

这是引发的错误。

我已经在我的tsconfig.json中包含了"jsx": "react"。我的配置如下:

{
      "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "lib": [
          "es2015",
          "dom"
      ],
      "moduleResolution": "node",
      "sourceMap": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "outDir": "build",
      "jsx": "react",
      "allowJs": true
  },
  "include": [
      "src/**/*.{ts,tsx}"
  ],
  "exclude": [
      "node_modules"
  ]
}
rsaldnfx

rsaldnfx1#

这解决了我的问题

"jsx": "react-jsx",

相关问题