简体   繁体   English

纱线创建带有打字稿抛出错误的反应应用程序

[英]yarn create react-app with typescript throwing errors

I am trying to create a new react app:我正在尝试创建一个新的 React 应用程序:

yarn create react-app my-app --template typescript

Installation seems to work fine, but when I try to yarn start the app, I get an error:安装似乎工作正常,但是当我尝试通过yarn start应用程序时,出现错误:

C:\\...\\my-app\\node_modules\\react-scripts\\scripts\\utils\\verifyTypeScriptSetup.js:239 appTsConfig.compilerOptions[option] = value; C:\\...\\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 '#' at verifyTypeScriptSetup (C:\\...\\my-app\\node_modules\\react-scripts\\scripts\\utils\\verifyTypeScriptSetup.js:239:43) at Object.类型错误:无法在 verifyTypeScriptSetup (C:\\...\\my-app\\node_modules\\react-scripts\\scripts\\utils\\verifyTypeScriptSetup.js:239:43) 处为对象“#”的只读属性“jsx”分配对象. (C:\\...\\my-app\\node_modules\\react-scripts\\scripts\\start.js:31:1) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47 error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. (C:\\...\\my-app\\node_modules\\react-scripts\\scripts\\start.js:31:1) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object。 Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules) /cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47 错误命令失败,退出代码1. info 访问 https://yarnpkg.com/en/docs/cli/run 获取有关此命令的文档。

There seems to be a problem with peer dependencies.对等依赖性似乎存在问题。 check-peer-dependencies lists unmet peer dependencies: check-peer-dependencies列出未满足的对等依赖项:

❌  @pmmmwh/react-refresh-webpack-plugin@0.4.2 requires type-fest ^0.13.1 (0.8.1 is installed) 
❌  @pmmmwh/react-refresh-webpack-plugin@0.4.2 requires webpack-hot-middleware 2.x (webpack-hot-middleware is not installed) 
❌  @pmmmwh/react-refresh-webpack-plugin@0.4.2 requires webpack-plugin-serve 0.x || 1.x (webpack-plugin-serve is not installed) 
❌  jest-config@26.6.1 requires ts-node >=9.0.0 (ts-node is not installed) 
❌  jsdom@16.4.0 requires canvas ^2.5.0 (canvas is not installed) 
❌  react-scripts@4.0.0 requires typescript ^3.2.1 (4.1.2 is installed) 
❌  sass-loader@8.0.2 requires fibers >= 3.1.0 (fibers is not installed) 
❌  sass-loader@8.0.2 requires node-sass ^4.0.0 (node-sass is not installed) 
❌  sass-loader@8.0.2 requires sass ^1.3.0 (sass is not installed) 
❌  ws@7.3.1 requires bufferutil ^4.0.1 (bufferutil is not installed) 
❌  ws@7.3.1 requires utf-8-validate ^5.0.2 (utf-8-validate is not installed)

After I add the unmet dependencies another error is thrown:添加未满足的依赖项后,抛出另一个错误:

error TS6046: Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'.错误 TS6046:'--jsx' 选项的参数必须是:'preserve'、'react-native'、'react'。

After I changed the value of from jsx": "react-jsx" to jsx": "react" in tsconfig.json I can finally start the app.之后,我从改变的值jsx": "react-jsx"jsx": "react"tsconfig.json我终于可以启动应用程序。

The errors happen with npx create-react-app as well. npx create-react-app 也会发生错误。 I tried to run the commands on windows 10 and linux - no differences.我尝试在 Windows 10 和 linux 上运行命令 - 没有区别。

What am I missing?我错过了什么?

donavon recommends a temporary solution: donavon推荐一个临时解决方案:

create a file in the root of your project:在项目的根目录中创建一个文件:

//verifyTypeScriptSetup.js
"use strict";
function verifyTypeScriptSetup () {}
module.exports = verifyTypeScriptSetup;

add a prestart in your package.json scripts在 package.json 脚本中添加prestart

"prestart": "cp verifyTypeScriptSetup.js node_modules/react-scripts/scripts/utils",

I share the link with the solution https://github.com/facebook/create-react-app/issues/9868#issuecomment-731396876我与解决方案https://github.com/facebook/create-react-app/issues/9868#issuecomment-731396876分享链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM