我使用
yarn create vite
命令创建 react-ts
应用程序并使用 vite
作为构建工具。 yarn add
安装软件包后,在 vite.config.ts
文件中:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
它给出错误:找不到模块“vite”,找不到模块“@vitejs/plugin-react”。当我尝试在 tsx 文件中导入
react, react-router-dom
时,它也会出现。但我已经使用yarn install
来安装package.json
文件中提到的所有依赖项(我也尝试过yarn add
,但没有成功)。
我尝试了以下解决方案,但都没有完全解决问题:
npm install / yarn add
缺少的模块错误仍然存在。
xxx.d.ts
然后声明模块丢失并在 include xxx.d.ts
中声明 ts.config
。它可以解决缺少模块的问题,但是
useState
和其他函数给出错误。
虽然 VS Code 报告了错误,但是项目可以运行并且可以工作。所以我想知道错误是否与配置文件或 ESLint 有关。
这是我的
package.json
:
{
"name": "client",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.22",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@types/react-router-dom": "^5.3.3",
"axios": "^0.25.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"typescript": "^4.5.5",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10",
"@vitejs/plugin-react": "^1.0.7",
"typescript": "^4.5.4",
"vite": "^2.8.0"
}
}
tsconfig.json
:
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": true,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
tsconfig.node.json
:
{
"compilerOptions": {
"composite": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["vite.config.ts"]
}
在“yarn”之后我无法取回 Node Modules 文件夹 我需要添加一个包含“nodeLinker:node-modules”的文件.yarnrc.yml 之后再次安装yarn(或npm)
这对我有用。
与我遇到的问题不完全相同。
但是,请尝试全局安装
vite
。
但最好的选择是卸载当前的
node
版本(并清理全局 node_modules
...),然后安装最新的 node
版本,实际上是节点 17
。