Reac+vite t打包部署后报错: require is not defined?-灵析社区

七厦

React+Antd打包部署之后报错: ReferenceError: require is not defined 看错误信息应该是react-dom文件里用到了require,但是路由好像没问题,是点击按钮的时候报错了,整个页面不能看 错误信息截图: ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241008/aae14b636b0792f1122f62d8d254cbac.png) vite.config.js代码: import { defineConfig,splitVendorChunkPlugin } from 'vite' import react from '@vitejs/plugin-react-swc' import { resolve } from "path" import { theme } from 'antd'; import lessToJs from 'less-vars-to-js'; import { readFileSync } from 'fs' import svgr from "vite-plugin-svgr"; const { defaultAlgorithm, defaultSeed } = theme; const varLessPath = "./theme/var.less" const mapToken = defaultAlgorithm(defaultSeed); const varLessStr = readFileSync(varLessPath, 'utf-8') const customVarLessJson = lessToJs(varLessStr, { resolveVariables: true, stripPrefix: true }); const prefixBase=process.env.ITLA_IMH_VIEW_ROUTERBASE; //https://vitejs.dev/config/ export default defineConfig({ base: prefixBase, build: { assetsDir:'statics', rollupOptions: { input: { // 配置所有页面路径,使得所有页面都会被打包 index: resolve(__dirname,'index.html') }, output:{ dir:'build', //file:'[name].html' } }, sourcemap:true }, define: { CUSTOMVARLESSDATA: `${JSON.stringify(customVarLessJson)}` }, plugins: [ // ReactRouterGenerator({ // outputFile: resolve(".", "./src/router/auto.jsx"), // isLazy: true, // comKey: "components" // }), react(), splitVendorChunkPlugin(), svgr({include: "**/*.svg?react",}) ], resolve: { alias: { "@": resolve(".", "./src"), "~": resolve(".", "./node_modules") }, extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', ".less",".jsonc"] }, css: { preprocessorOptions: { less: { modifyVars: { ...mapToken, ...customVarLessJson }, javascriptEnabled:true, }, }, modules:{ localsConvention:"camelCase", generateScopedName:'[name]_[local]_[hash:6]', //globalModulePaths:['./theme/*.scss'] } }, server: { port: 3187, open: false, host: true, proxy: { '^/api/imh': { // target: "http://localhost:8088", //target: "http://10.14.0.26:8088", //target: "http://10.14.0.147:8088", //target: "http://10.14.0.116:8088", //target: "http://192.168.120.11:8811", target: "http://10.14.0.61:8812", //target: "http://10.14.0.223:9999", //target: "http://192.168.120.11:8812", changeOrigin: true, rewrite: (path) => { return path.replace("/api/imh", "/imh") } }, }, }, envPrefix: "ITLA_IMH_VIEW_", }) 期望消除错误

阅读量:121

点赞量:0

问AI
按钮的点击回调中,你做了什么?贴出来看看