英文:
Problem with build hooks imports and building on any host
问题
我在Vercel上构建项目时遇到了问题。它在我的本地机器上成功构建,但是当我部署到任何主机上时,我遇到了一个导入问题,我无法解决。
以下是在构建过程中收到的错误消息:
[12:07:15.658] src/Components/PlatformSelector/PlatformSelector.tsx(5,26): error TS2307: 无法找到模块'../../Hooks/usePlatforms'或其相应的类型声明。
[12:07:15.659] src/Hooks/usePlatform.ts(1,26): error TS2307: 无法找到模块'./usePlatforms'或其相应的类型声明。
[12:07:15.694] 错误: 命令“npm run build”退出代码2
这是usePlatform
钩子的代码:
import Platform from "../Entities/Platform";
import usePlatforms from "./usePlatforms";
const usePlatform = (id?: number) => {
const {data: platforms} = usePlatforms();
return platforms?.results.find((p: Platform) => p.id === id)
}
export default usePlatform;
这是usePlatforms
钩子的代码:
import { useQuery } from '@tanstack/react-query';
import ms from 'ms';
import platforms from '../data/platforms';
import Platform from '../entities/Platform';
import APIClient from '../services/api-client';
const apiClient = new APIClient<Platform>(
'/platforms/lists/parents'
);
const usePlatforms = () =>
useQuery({
queryKey: ['platforms'],
queryFn: apiClient.getAll,
staleTime: ms('24h'),
initialData: platforms,
});
export default usePlatforms;
最后,这是PlatformSelector
组件的代码:
import { Button, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { BsChevronDown } from "react-icons/bs";
import Platform from "../../Entities/Platform";
import usePlatform from "../../Hooks/usePlatform";
import usePlatforms from "../../Hooks/usePlatforms";
import useGameQueryStore from "../../store";
const PlatformSelector = () => {
const setPlatformId = useGameQueryStore((state) => state.setPlatformId);
const selectedPlatformId = useGameQueryStore(
(state) => state.gameQuery.platformId
);
const selectedPlatform = usePlatform(selectedPlatformId);
const { data, error } = usePlatforms();
if (error) return null;
return (
<Menu>
<MenuButton as={Button} rightIcon={<BsChevronDown />}>
{selectedPlatform?.name || "Platforms"}
</MenuButton>
<MenuList>
{data?.results.map((platform: Platform) => (
<MenuItem
onClick={() => setPlatformId(platform.id)}
key={platform.id}
>
{platform.name}
</MenuItem>
))}
</MenuList>
</Menu>
);
};
export default PlatformSelector;
之前,我遇到了usePlatforms
钩子的大小写问题,最初它以大写字母命名。我更改了标题并像这样调整了TypeScript配置:
"forceConsistentCasingInFileNames": false
我收到了上述提到的错误。如何解决这个问题?
英文:
I'm having trouble building my project on Vercel. It builds successfully on my local machine, but when I deploy it on any host, I encounter an import issue that I can't figure out.
Here is the error message I receive during the build process:
[12:07:15.658] src/Components/PlatformSelector/PlatformSelector.tsx(5,26): error TS2307: Cannot find module '../../Hooks/usePlatforms' or its corresponding type declarations.
[12:07:15.659] src/Hooks/usePlatform.ts(1,26): error TS2307: Cannot find module './usePlatforms' or its corresponding type declarations.
[12:07:15.694] Error: Command "npm run build" exited with 2
Here is the code for the usePlatform
hook:
import Platform from "../Entities/Platform";
import usePlatforms from "./usePlatforms";
const usePlatform = (id?: number) => {
const {data: platforms} = usePlatforms();
return platforms?.results.find((p: Platform) => p.id === id)
}
export default usePlatform;
And here is the code for the usePlatforms
hook:
import { useQuery } from '@tanstack/react-query';
import ms from 'ms';
import platforms from '../data/platforms';
import Platform from '../entities/Platform';
import APIClient from '../services/api-client';
const apiClient = new APIClient<Platform>(
'/platforms/lists/parents'
);
const usePlatforms = () =>
useQuery({
queryKey: ['platforms'],
queryFn: apiClient.getAll,
staleTime: ms('24h'),
initialData: platforms,
});
export default usePlatforms;
Finally, here is the code for the PlatformSelector
component:
import { Button, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { BsChevronDown } from "react-icons/bs";
import Platform from "../../Entities/Platform";
import usePlatform from "../../Hooks/usePlatform";
import usePlatforms from "../../Hooks/usePlatforms";
import useGameQueryStore from "../../store";
const PlatformSelector = () => {
const setPlatformId = useGameQueryStore((state) => state.setPlatformId);
const selectedPlatformId = useGameQueryStore(
(state) => state.gameQuery.platformId
);
const selectedPlatform = usePlatform(selectedPlatformId);
const { data, error } = usePlatforms();
if (error) return null;
return (
<Menu>
<MenuButton as={Button} rightIcon={<BsChevronDown />}>
{selectedPlatform?.name || "Platforms"}
</MenuButton>
<MenuList>
{data?.results.map((platform: Platform) => (
<MenuItem
onClick={() => setPlatformId(platform.id)}
key={platform.id}
>
{platform.name}
</MenuItem>
))}
</MenuList>
</Menu>
);
};
export default PlatformSelector;
Previously, I had a problem with the case of the usePlatforms
hook, as it was initially named with a capital letter. I changed the title and adjusted the TypeScript configuration like this:
"forceConsistentCasingInFileNames": false
I'm receiving the error mentioned above. How can I resolve this issue?
答案1
得分: 1
你可能会在Vercel上遇到导入错误,其中文件系统区分大小写。
在你的代码中,例如:
// usePlatform hook
import Platform from "../Entities/Platform";
// usePlatforms hook
import Platform from '../entities/Platform';
你需要确保你的文件名和导入语句在大小写上完全匹配。
英文:
You might encounter an import error on Vercel, where the filesystem is case-sensitive.
In your code, for example:
// usePlatform hook
import Platform from "../Entities/Platform";
// usePlatforms hook
import Platform from '../entities/Platform';
You need to make sure that your filenames and import statements match exactly in letter-casing.
答案2
得分: 0
我不要回答我要翻译的问题。以下是要翻译的内容:
我不得不使用git config core.ignorecase false来使git看到大小写的更改。在我使用它并更改了一些文件和导入的大小写之后,一切正常。
英文:
I had to use git config core.ignorecase false to make git see changes in casing. After I used it and change casing in some files and imports , everything works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论