Failed to load module script: Expected a JavaScript module script but the server

1,366 阅读1分钟

react vite 报错

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.

Uncaught TypeError: Failed to fetch dynamically imported module: http://localhost:5173/pages/business-manage/case-manage/case-query/index.tsx

request.ts

`/api/${_url}`

vite.config.js

proxy:{
'/api': {
      target,
      changeOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, ''),
    }
}

原因

报错的index.tsx文件中引用了

import * as API from '@/api';

原本是请求ts文件,但由于文件名也是api,导致被代理匹配到并拦截了

解决方法

更改匹配规则

proxy:{
'^/api/[^.]*$': {
      target,
      changeOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, ''),
    }
}