ค้นหาเว็บไซต์

reactjs - yarn create react-app กับ typescript ทำให้เกิดข้อผิดพลาด


ฉันกำลังพยายามสร้างแอป react ใหม่:

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

การติดตั้งดูเหมือนจะทำงานได้ดี แต่เมื่อฉันพยายาม yarn start แอป ฉันได้รับข้อผิดพลาด:

C:\...\my-app\node_moduleseact-scripts\scripts\utils\verifyTypeScriptSetup.js:239 appTsConfig.compilerOptions[option]=value; ^

TypeError: ไม่สามารถกำหนดค่าให้กับ property ที่อ่านอย่างเดียว 'jsx' ของ object '#' ที่ verifyTypeScriptSetup (C:\...\my-app\node_moduleseact-scripts\scripts\utils\verifyTypeScriptSetup.js:239:43) ที่ Object. (C:\...\my-app\node_moduleseact-scripts\scripts\start.js:31:1) ที่ Module._compile (internal/modules/cjs/loader.js:1063:30) ที่ Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) ที่ Module.load (internal/modules/cjs/loader.js:928:32) ที่ Function.Module._load (internal/modules/cjs/loader.js:769:14) ที่ Function.executeUserEntryPoint [เป็น runMain] (internal/modules/run_main.js:72:12) ที่ internal/main/run_main_module.js:17:47 ข้อผิดพลาด Command ล้มเหลวด้วย exit code 1. info เยี่ยมชม https://yarnpkg.com/en/docs/cli/run สำหรับเอกสารเกี่ยวกับคำสั่งนี้.

ดูเหมือนว่าจะมีปัญหาเกี่ยวกับ peer dependencies check-peer-dependencies แสดงรายการ peer dependencies ที่ไม่ตรงตามเงื่อนไข:

❌  @pmmmwh/[email  requires type-fest ^0.13.1 (0.8.1 is installed) 
❌  @pmmmwh/[email  requires webpack-hot-middleware 2.x (webpack-hot-middleware is not installed) 
❌  @pmmmwh/[email  requires webpack-plugin-serve 0.x || 1.x (webpack-plugin-serve is not installed) 
❌  [email  requires ts-node >=9.0.0 (ts-node is not installed) 
❌  [email  requires canvas ^2.5.0 (canvas is not installed) 
❌  [email  requires typescript ^3.2.1 (4.1.2 is installed) 
❌  [email  requires fibers >= 3.1.0 (fibers is not installed) 
❌  [email  requires node-sass ^4.0.0 (node-sass is not installed) 
❌  [email  requires sass ^1.3.0 (sass is not installed) 
❌  [email  requires bufferutil ^4.0.1 (bufferutil is not installed) 
❌  [email  requires utf-8-validate ^5.0.2 (utf-8-validate is not installed)

หลังจากที่ฉันเพิ่มการพึ่งพาที่ยังไม่ถูกตอบสนอง ข้อผิดพลาดอื่นก็เกิดขึ้น:

ข้อผิดพลาด TS6046: อาร์กิวเมนต์สำหรับตัวเลือก '--jsx' ต้องเป็น: 'preserve', 'react-native', 'react'

หลังจากที่ฉันเปลี่ยนค่าจาก jsx": "react-jsx" เป็น jsx": "react" ใน tsconfig.json ตอนนี้ฉันสามารถเริ่มแอปได้แล้ว

ข้อผิดพลาดเกิดขึ้นกับ npx create-react-app ด้วยเช่นกัน ผมลองรันคำสั่งบน Windows 10 และ Linux แล้ว - ไม่มีความแตกต่าง

ฉันกำลังพลาดอะไรอยู่หรือ?

คำตอบ #1

ดอนาวอน แนะนำวิธีแก้ปัญหาชั่วคราว:

สร้างไฟล์ในโฟลเดอร์หลักของโปรเจกต์ของคุณ:

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

เพิ่ม prestart ในสคริปต์ของคุณในไฟล์ package.json

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

ฉันแชร์ลิงก์ที่มีวิธีแก้ปัญหา https://github.com/facebook/create-react-app/issues/9868#issuecomment-731396876