You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用npx create-react-app创建TypeScript项目失败,改扩展名遇文件缺失错误

问题解决步骤

1. 补全TypeScript配置文件

如果项目根目录没有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"]
}

2. 检查package.json配置

打开项目根目录的package.json,确认scripts字段未硬指定入口文件,正常配置应为:

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
}

3. 确保入口文件正确

  • 确认src目录下的入口文件是index.tsx
  • 检查public/index.html内的脚本引用,无需手动指定index.js,React会自动识别TS入口

4. 手动添加TypeScript依赖

若以上操作无效,直接在现有项目中安装TS相关依赖:

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

安装完成后重启开发服务器即可。

5. 清理缓存重试

  • 删除依赖目录和锁文件:
rm -rf node_modules package-lock.json
  • 重新安装依赖并启动:
npm install && npm start

内容的提问来源于stack exchange,提问作者farideh rajabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 04:14:56