reactjs npm run build error with vite(typescript)

4ktjp1zp  于 2023-05-22  发布在  React
关注(0)|答案(3)|浏览(187)

错误

node_modules/@types/react-router-dom/index.d.ts:13:10 - error TS2305: Module '"react-router"' has no exported member 'match'.
node_modules/@types/react-router-dom/index.d.ts:19:5 - error TS2305: Module '"react-router"' has no exported member 'PromptProps'.
node_modules/@types/react-router-dom/index.d.ts:20:5 - error TS2305: Module '"react-router"' has no exported member 'Prompt'.
node_modules/@types/react-router-dom/index.d.ts:23:5 - error TS2305: Module '"react-router"' has no exported member 'RedirectProps'.
node_modules/@types/react-router-dom/index.d.ts:24:5 - error TS2305: Module '"react-router"' has no exported member 'Redirect'.
node_modules/@types/react-router-dom/index.d.ts:25:5 - error TS2305: Module '"react-router"' has no exported member 'RouteChildrenProps'.
node_modules/@types/react-router-dom/index.d.ts:26:5 - error TS2305: Module '"react-router"' has no exported member 'RouteComponentProps'.
node_modules/@types/react-router-dom/index.d.ts:31:5 - error TS2305: Module '"react-router"' has no exported member 'StaticRouterProps'.
node_modules/@types/react-router-dom/index.d.ts:32:5 - error TS2305: Module '"react-router"' has no exported member 'StaticRouter
node_modules/@types/react-router-dom/index.d.ts:33:5 - error TS2305: Module '"react-router"' has no exported member 'SwitchProps'.
node_modules/@types/react-router-dom/index.d.ts:34:5 - error TS2305: Module '"react-router"' has no exported member 'Switch'.
node_modules/@types/react-router-dom/index.d.ts:35:5 - error TS2305: Module '"react-router"' has no exported member 'match'.
... same errors but different members

node_modules/@types/react-router/index.d.ts:189:53 - error TS2694: Namespace '"C:/Users/user/Desktop/app/frontend/node_modules/history/index"' has no exported member 'LocationState'.
189 export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;
node_modules/@types/react-router/index.d.ts:189:71 - error TS2315: Type 'History' is not generic.
189 export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;                     
node_modules/@types/react-router/index.d.ts:191:35 - error TS2694: Namespace '"C:/Users/user/Desktop/app/frontend/node_modules/history/index"' has no exported member 'LocationState'.
191 export function useLocation<S = H.LocationState>(): H.Location<S>;                                   
node_modules/@types/react-router/index.d.ts:191:53 - error TS2315: Type 'Location' is not generic.
191 export function useLocation<S = H.LocationState>(): H.Location<S>;
... again more errors

包.json

{
  "name": "frontend",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@reduxjs/toolkit": "^1.7.0",
    "@types/react-redux": "^7.1.20",
    "@types/react-router-dom": "^5.3.2",
    "axios": "^0.24.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.6",
    "react-router-dom": "^6.1.0"
  },
  "devDependencies": {
    "@types/react": "^17.0.33",
    "@types/react-dom": "^17.0.10",
    "@vitejs/plugin-react": "^1.0.7",
    "typescript": "^4.4.4",
    "vite": "^2.7.0"
  }
}

每当我尝试构建时,它都会向我显示所有这些我以前从未遇到过的错误,所以不知道为什么会发生这种情况......,第一部分与ts有关,所以可以修复,但第二部分没有意义,任何帮助都很感激!

smtd7mpg

smtd7mpg1#

我也有同样的问题。当我处于开发模式时,我的环境可以工作,但是当我通过vite开始构建时,我得到了一些react-router-dom错误。
看起来@types/react-router-dom还不能用于react-router v6。目前,https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-router-dom/index.d.ts显示Type definitions for react-router-dom 5.3
因此,可以选择降级到react-router v5,或者等到v6的类型可用。

7lrncoxx

7lrncoxx2#

也许你可以尝试将"skipLibCheck": true添加到tsconfig.json。

{
  "compilerOptions": {
    // ...
    "skipLibCheck": true
  }
}
j1dl9f46

j1dl9f463#

我也遇到了同样的问题,所以我更新了我的 typescript 和它的工作:)

  • 打包器模块分辨率是一个新功能,仅适用于Typescript 5.x。尝试使用以下命令更新最新的 typescript 。*
npm update typescript -g 
 or
npm install typescript@latest -g

检查:https://github.com/vitejs/vite/issues/13129

相关问题