找不到模块“@vitejs/plugin-react”或其对应类型
我使用 npm create vite@latest
创建了项目,并选择 ts-react,当我运行脚本时 npm run dev
工作了,但是没有警告,我在 vs 的 vite.config.ts 中收到 Cannot find module '@vitejs/plugin-react' or its相应类型
代码。
I have created the project using npm create vite@latest
and I choose ts-react, when I ran the script thenpm run dev
worked, no warning however, I am getting an Cannot find module '@vitejs/plugin-react' or its corresponding type
in vite.config.ts in vs code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试重新启动 VSCode 编辑器
Try to restart your VSCode editor
请按照以下步骤重新启动 TS Server:
Command
+Shift
+P
。TypeScript:重新启动 TS 服务器
。Enter
。Restart your TS Server following these steps:
Command
+Shift
+P
.TypeScript: Restart TS server
.Enter
.如果您使用的是 SWC!
使用
"@vitejs/plugin-react-swc"
,而不是"@vitejs/plugin-反应”。
If you are using SWC!
Use
"@vitejs/plugin-react-swc"
, instead of"@vitejs/plugin-react"
.问题本身就暗示了问题是什么。您需要安装“@vitejs/plugin-react”。由于某种原因,当您创建 vite 项目时它不会被包含在内。
如果你使用yarn
如果你使用npm
The question itself is hinting what the issue is. You need to install '@vitejs/plugin-react'. For some reason it doesn't get included when you create a vite project.
If you're using yarn
if you're using npm
就我而言,问题是我忘记在初始创建项目( npm create vite@latest 或纱线创建 vite )。
因为vite需要安装package.json中的所有依赖并创建node_modules文件夹
In my case the problem was that I forgot to run yarn in terminal ( or npm install if you use npm) just after initial creation of project ( npm create vite@latest or yarn create vite ).
Because vite needs to install all dependencies in package.json and create node_modules folder
就我而言,我忘记安装类型:
$ npm i -D @types/node
In my case it was that I forgot to install types:
$ npm i -D @types/node
在我的 tsconfig.json 文件中添加 "types": ["vite"] 对我有用。
Adding "types": ["vite"] in my tsconfig.json file worked for me.
所有解决方案都不适合我!
当我导航到文件夹时:
在我的项目中显示,没有
vite.ts
文件,这非常奇怪。复制
vite.ts
文件进入父目录,为我修复它。不确定我的安装过程中出了什么问题。
编辑:最后我从 \src\ 复制了所有文件,因为
vite.ts
有很多依赖项All solutions didn't work for me!
When I was navigating to the folder:
in my project it showed, that there is no
vite.ts
file which is very weired.Copying the
vite.ts
file frominto the parent directory, fixed it for me. Not sure what went wrong during my installation process.
Edit: In the end I copied all files from \src\ since
vite.ts
has many dependencies