【问题标题】:Problem with Visual Studio Code using "react-jsx" as jsx value with create-react-app使用“react-jsx”作为 jsx 值和 create-react-app 的 Visual Studio Code 问题
【发布时间】:2021-03-06 12:51:53
【问题描述】:

我正在努力解决 VSCode 中的“错误”:

Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'

因此,当 react 值似乎有效时,react-scripts (create-react-app) 会自动将 jsx 键设置为 react-jsx 值。

实际上,代码可以完美运行并显示我想要的页面,但是 IDE 将所有内容都强调为错误,说:

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

这是我的 tsconfig.json :

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

,还有我的 package.json(默认由 create-react-app 提供 + 包更新):

{
  "name": "front-office",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.6",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.2.2",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.9",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "bootstrap": "^4.5.3",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我使用的是最新版本的 Typescript,我使用 ncu 更新了我的所有包,关闭/打开了 VSCode(有时与 tsconfig 一起使用!),似乎没有任何解决办法。

我很确定这是一个 VSCode 问题,但我对如何解决这个问题没有任何想法。

你们中有人有想法吗?

编辑:

如果您按照上述步骤操作并且 VSCode 仍然显示“-jsx”错误,请确保您已禁用“TypeScript God”扩展(以及任何其他 TS 扩展,直到问题不再出现)。

【问题讨论】:

    标签: typescript visual-studio-code jsx create-react-app tsconfig


    【解决方案1】:

    这是因为 VSCode 的 Typescript 版本没有使用 create-react-app 默认使用的最新 babel 功能。

    您可以change VS Code to use the workspace's version of Typescript instead,这将解决此问题。

    打开 TypeScript 或 JavaScript 文件,然后单击状态栏中的 TypeScript 版本号。将出现一个消息框,询问您应该使用哪个版本的 TypeScript VS Code

    选择“使用工作区版本”以使用较新的 Create React App 打字稿版本。

    【讨论】:

    • 这为我解决了这个问题。
    • 似乎是它与我使用的扩展(TypeScript God)之间的混合,可能不是最新的。无论如何,它解决了我的问题,谢谢!
    • 自上次保存我的 a** 以来的天数:0
    • 如果项目被拆分为多个子文件夹,如何更改工作区版本? VSCode 似乎没有在子目录中获取node_modules,或者至少我没有看到其他可用的版本
    • @Narigo 我全局安装了我想要的版本 (npm i -g typescript@4.1.2) 然后将此行添加到 vscode 设置 json 文件 "typescript.tsdk": "/home/yourusername/.nvm/versions/node/v10.15.1/lib/node_modules/typescript/lib" 替换 /home/yourusername/.nvm/versions/node/v10.15.1/lib/node_modules 为您的全局 npm node_modules 文件夹路径(用 @ 查找987654326@)
    【解决方案2】:

    对于使用 VS 2019 的用户,我必须从这里为 Visual Studio 安装 typescript 4.1: https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.TypeScript-41

    然后通过在 compilerOptions 等中使用新的 jsx 设置,它工作正常。

    【讨论】:

      【解决方案3】:

      在 PhpStorm(WebStorm 可能也是如此)中,通过在远程 JSON 模式中勾选“始终下载最新版本的模式”,我得到它不会抱怨“react-jsx”作为值。

      【讨论】:

      • 我相信问题是关于 vscode ....
      • 是的,因为我自己在搜索 PhpStorm 时发现了这个问题,我想也许这也可以帮助其他人。这也有助于成为有关 vscode 的线索,即该问题可能与 JSON 模式过时有关。
      • 这确实帮助了我,因为我正在使用 WebStorm。谢谢!
      【解决方案4】:

      如果您使用的是 VS Code 并且 Marksfrancis 的回答对您不起作用。也许你应该检查你的 TypeScript 扩展,在我的例子中,扩展“TypeScript God”是我仍然遇到这个问题的原因。

      【讨论】:

        【解决方案5】:

        在 VSC 中安装 JavaScript and TypeScript Nightly extension 并重新选择 TS 版本(打开 .tsx 时,单击 TS 版本(右下角),然后“选择 TypeScript 版本”->“使用 VS Code 的版本”)也可以。

        (src:https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-typescript-nightly-builds)

        【讨论】:

          【解决方案6】:

          如果您尝试通过在 vscode 编辑器中选择“使用工作区版本”来更新 typescript 版本,但仍然显示错误,请尝试将 .tsconfig 文件中字段 "jsx" 的值更新为 "preserve"

          • 这将使您能够以旧方式编写 JSX。
          import React from 'react';
          
          function App() {
            return <h1>Hello World</h1>;
          } 
          
          • 如果使用值"react-jsx" 值,则必须在新的 React 17 Transform 中编写 react:
          import { jsx as _jsx } from "react/jsx-runtime";
          export const helloWorld = () => _jsx("h1", { children: "Hello world" }, void 0);
          

          .tsconfig 文件参考(根据 typescript 文档):here

          新的 JSX 转换(根据反应文档):here

          【讨论】:

            【解决方案7】:

            正如其他人所提到的,这是 create-react-app (CRA) 脚本的一个未解决问题。但是,上述解决方案都不适合我。唯一适合我的解决方案是在.env 文件(在项目根目录中)中添加以下配置。

            DISABLE_NEW_JSX_TRANSFORM=true
            

            【讨论】:

              【解决方案8】:

              只是替换

               "jsx": "react-jsx"
              

              "jsx": "react"
              

              干杯!!

              【讨论】:

              • 原发帖人已经提到他们知道"jsx": "react" 有效,所以这个答案丝毫没有帮助。此外,即使您手动将"react-jsx" 替换为"react",CRA 脚本也会在您每次运行代码时自动将其改回"react-jsx"
              猜你喜欢
              • 2021-01-21
              • 2017-06-24
              • 1970-01-01
              • 2021-02-28
              • 2021-07-13
              • 2019-05-29
              • 1970-01-01
              • 1970-01-01
              • 2021-04-27
              相关资源
              最近更新 更多