在我的开发环境中,项目运行完美。但是当我尝试将其部署到
vercel
时,它无法构建并给出错误:
src/App.tsx(9,18):错误 TS2307:找不到模块“./pages/Home”或其相应的类型声明。 src/App.tsx(11,19):错误 TS2307:找不到模块“./pages/Login”或其相应的类型声明。 src/App.tsx(13,22):错误 TS2307:找不到模块“./pages/Archived”或其相应的类型声明。
这是我的 tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
还有我的 tsconfig.node.json:
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
我尝试添加这些:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"]
}
但这不起作用。我还修改了 vite.config.ts 添加:
resolve: {
alias: {
'@pages' : '/src/pages',
},
},
这个问题与绝对路径有关。请检查其他问题以及设置绝对路径的方法。
tsconfig.json
{
"compilerOptions": {
...,
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["vite.config.ts"]
}
"@/": ["./src/"] > 表示“@”替换“./src/”
但是,你也让Vite理解了绝对路径。
所以,这种选项需要vite.config.json
resolve: {
alias: {
'@' : './src/*',
},
},
(It might be wrong)
谢谢您的意见,但我昨天已经解决了这个问题。对于可能经历与我昨天相同情况的人来说,首先要真正理解错误日志并彻底阅读。我遇到的问题是 vercel 创建了一个单独的“Pages”目录(大写目录),同时也有我的版本的“pages”(小写目录)。它首先读取“Pages”目录,并且没有运行我的“pages”版本。为了检查这一点,vercel 拥有源代码的副本。解决方案是我删除了我的“页面”版本并首先提交/推送更改以进行新的部署,然后 vercel 保存更改(首先您需要制作目录的副本)。然后您需要将复制的目录放到正确的路径并再次推送更改。
Disclaimer:
这可能并不适合所有人。