Vite v5.3.2生产构建报TS6142错误:未设置--jsx参数
TS6142错误原因及解决方案
错误原因
TS6142错误本质是TypeScript编译器在解析.tsx模块时,未加载到包含jsx编译选项的配置文件:
- 根目录
tsconfig.json采用**项目引用(Project References)**结构,自身无jsx配置,也未直接包含src目录文件;单独执行tsc时默认读取该根配置,无法继承tsconfig.app.json中的jsx: "react-jsx"规则。 tsconfig.app.json中同时存在files和include字段,files会覆盖include的作用,导致src目录文件未被正确纳入编译范围。
解决步骤
1. 修正build脚本,指定tsconfig文件
修改package.json中的build脚本,让tsc明确加载包含jsx配置的tsconfig.app.json:
"scripts": { "build": "tsc -p tsconfig.app.json && vite build" }
2. 修复tsconfig.app.json的文件范围配置
删除tsconfig.app.json中的files字段,避免覆盖include的作用,同时将dist移至exclude排除编译输出目录:
{ "compilerOptions": { "composite": true, "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", "target": "ES2020", "useDefineForClassFields": true, "lib": ["ES2020", "DOM", "DOM.Iterable"], "module": "ESNext", "skipLibCheck": true, "moduleResolution": "bundler", "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, "moduleDetection": "force", "noEmit": true, "jsx": "react-jsx", "jsxImportSource": "@emotion/react", "types": ["vite/client", "@emotion/react/types/css-prop"], "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true }, "include": ["src", "emotion.d.ts", "svg.d.ts"], "exclude": ["node_modules", "dist"] }
3. 验证执行
运行npm run build,此时tsc会正确读取jsx配置,处理.tsx模块,TS6142错误将被消除。
内容的提问来源于stack exchange,提问作者jaeyo
相关产品推荐
相关产品推荐

