create-react-app typescript 将不会运行 npm start

2024-04-02

我在尝试着create-react-app使用 typescript,但 JSX 和 TS 似乎有问题。我读了一些可能的解决方案,但对我来说并不奏效。

I have:

  • npm:版本 16.14.7
  • 节点:版本14.8.0

I used npx create-react-app my-app --template typescript来自docs https://create-react-app.dev/docs/adding-typescript/#installation

安装很顺利,但是当我尝试运行时npm start它给了我这个错误:

> [email protected] /cdn-cgi/l/email-protection start /Users/lukasbenediktson/Desktop/react-types/my-app
> react-scripts start

/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'
    at verifyTypeScriptSetup (/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)
    at Object.<anonymous> (/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react-scripts/scripts/start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1251:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
    at Module.load (internal/modules/cjs/loader.js:1100:32)
    at Function.Module._load (internal/modules/cjs/loader.js:962:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] /cdn-cgi/l/email-protection start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] /cdn-cgi/l/email-protection start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我试图改变这一行 238node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js as tudor07 在这里建议 https://github.com/facebook/create-react-app/issues/9868#issuecomment-723576740:

} else if (parsedCompilerOptions[option] !== valueToCheck) {

to

} else if (parsedCompilerOptions[option] !== valueToCheck && option !== "jsx") {

这让我可以奔跑npm start服务器已启动并正在运行。但现在显示它无法识别 JSX/TSX:

TypeScript error in /Users/lukasbenediktson/Desktop/react-types/my-app/src/App.tsx(7,5):
Could not find a declaration file for module 'react/jsx-runtime'. '/Users/lukasbenediktson/Desktop/react-types/my-app/node_modules/react/jsx-runtime.js' implicitly has an 'any' type.
  If the 'react' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react`  TS7016

     5 | function App() {
     6 |   return (
  >  7 |     <div className="App">
       |     ^
     8 |       <header className="App-header">
     9 |         <img src={logo} className="App-logo" alt="logo" />
    10 |         <p>

My package.json看起来像这样:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-scripts": "4.0.0",
    "typescript": "^4.0.3",
    "web-vitals": "^0.2.4"
  },
  "devDependencies": {
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.53",
    "@types/react-dom": "^16.9.8"
  },
  "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"
    ]
  }
}

感谢您的时间。

更新:解决方案(至少对我来说)

In my tsconfig.json我改变了我的"compilerOptions": from "jsx": "react-jsx" to "jsx": "react"。请注意,我仍然需要对我的文件中的第 238 行进行更改node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js as tudor07 在这里建议 https://github.com/facebook/create-react-app/issues/9868#issuecomment-723576740

如果它太“hacky”,请告诉我


就我而言,@captain-yossarian 说得对。过时的类型(@types/[email protected] /cdn-cgi/l/email-protection) 给定指定版本的 React ([email protected] /cdn-cgi/l/email-protection)。修理:

npm install --upgrade @types/react@17
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

create-react-app typescript 将不会运行 npm start 的相关文章

  • 如何将React版本17降级到16?

    由于 React 17 不支持许多 React 包 如何将 React 版本从 17 降级到 16 假设您使用 npm yarn 或其他节点包管理器 依赖项版本列在package json 搜索react and react dom包下de
  • Typescript:类类型的实例

    我的课程类型如下A Type class A constructor public a string type A Type new a string A 我想获取实例的类型A Type构造函数 以便我可以显式键入该函数 class A c
  • React Native 反应导航 SafeArea 问题

    我更新了我的android项目 react navigation was 3 x 搬去5 x 决定实施必要的更改 但它不想工作 从反应导航页面复制了示例 但仍然显示相同的错误 有人知道问题出在哪里吗 当我用谷歌搜索一半的互联网时找不到解决方
  • Angular 4+为ngComponentOutlet动态创建的组件分配@Input

    在 Angular 4 中动态创建一个可以使用的组件ngComponentOutlet指示 https angular io docs ts latest api common index NgComponentOutlet directi
  • 内容丰富的 api markdown 转换为 HTML

    有没有什么简单的方法可以将 Markdown 文本从 Contentful api 转换为 html 代码以显示在 html 页面上 我尝试过使用 pagedown 和一些类似的技术 但似乎没有一个对我有用 我是 Contentful 的客
  • 任何 JavaScript 代码都是有效的 TypeScript 代码吗?

    目前我已经开始学习TypeScript 从我研究过的文档来看TypeScript 我看到一些纯的样品JavaScript代码可以编译为TypeScript code 我的问题是 TypeScript 语言的设计方式是否使任何 JavaScr
  • React-Router v5 History.push 不触发组件

    很抱歉朋友们又问这个问题了 我找到了解决这个问题的几个方法 但他们让我很困惑 我只是想问一个优雅的方法来做到这一点 正如你通过标题所理解的 我有一个关于反应路由的问题 让我解释 我有一个按钮 其名称是Login 单击按钮后 我执行了一些逻辑
  • 为什么react中的组件需要大写? [复制]

    这个问题在这里已经有答案了 因此 当您声明一个组件以小写首字母进行反应时 它不会显示 也不会引发错误 当您将组件名称大写时 它确实有效 这样的实现是怎样的呢 为了避免与现有的 html 元素发生冲突 还是这是一个错误 var test Re
  • 使用 Typescript 检查接口类型

    这个问题是直接类比使用 TypeScript 检查类类型 https stackoverflow com questions 12789231 class type check with typescript 我需要在运行时查明任何类型的变
  • 如何使用 forkjoin 返回 Angular 中两个独立 API 调用的组合响应

    我有两个独立的 api 调用 下面是两个api调用的代码 public getLendingType partyId string Observable lt lendingType string partyId string gt ret
  • 在设定的时间后从 DOM 中删除元素

    我正在尝试找出在事件触发后从 DOM 中删除元素的 React 方法 我正在尝试发出警报 copySuccess when onClick this props handleCopyFact 被触发 然后在 5 秒后淡出该警报 每个组件的状
  • 如何使用 setState 插入 React 的状态数组?

    我正在寻找在反应中修改和数组并在特定索引上插入元素 这就是我的状态 this state arr 我想做的是编译这个arr index random element 反应 js setState 语法 我试图做的是 this setStat
  • Typescript:具有排除更改属性修饰符的映射类型

    当使用 Minus 映射类型时 它似乎从属性中删除修饰符 我认为这是由排除类型引起的 但我不确定为什么 我期望 Minus 只从 T 中删除 U 的键 而不更改 T 的属性修饰符 type Minus
  • React hooks 状态变量在重新渲染后未更新

    在下面的示例中 我有一个想要更新的元素 水果 数组 并使用更新后的数组执行其他操作 在本例中保存更新后的列表 我的理解是重新渲染状态将更新 但它不在这里 或者状态更新和我的操作之间存在延迟 In the addFruit功能我可以看到 Pe
  • Ionic 2 RC0 和 Angular 2 最新的 Android 构建错误(ngc:错误:静态解析符号值时遇到错误)

    当我使用构建android时出现错误ionic build android命令 ngc 错误 静态解析符号值时遇到错误 引用本地 非导出 符号 字典 考虑导出符号 原始 ts文件中的位置14 8 解析符号TRANSLATION PROVID
  • 如何以编程方式枚举枚举类型?

    假设我有一个 TypeScriptenum MyEnum 如下 enum MyEnum First Second Third TypeScript 0 9 5 中生成数组的最佳方法是什么enum价值观 例子 var choices MyEn
  • 如何在 TypeScript 中使可为 null 的属性可选?

    我有这样的类型 type City name string id number null 并想把它变成这样的类型 type City name string id number 我已经看到很多帖子解释如何使可选类型可以为空 但我想要相反的东
  • 如何使用 TypeScript 接口输入 Vue 提示 props?

    这件事真的让我的头撞到了墙上 我记得在 Angular 工作时 TS 接口可用于输入提示参数 我想对 Vue 中的 props 做同样的事情 有任何想法吗 代码如下 但仅针对标准对象进行检查 因此传入任何对象都是有效的 import Vue
  • ‘state’未定义 no-undef

    我使用教程来学习 React 但我很快就陷入困境 在教程中 他们使用以下代码 import React Component from react class Counter extends Component state count 0 r
  • 深层嵌套 Flexbox 布局是否会导致性能问题?

    我一直在开发一个 ReactJS 项目 其中我使用 Flexbox 布局创建了大部分组件 由于使用 React 我们可以拥有深度嵌套的组件 因此我的布局具有嵌套的 Flexbox 布局 现在我的问题是 这对性能有任何问题吗 在一个页面上 有

随机推荐