从Vite迁移至Rsbuild时遇内存分配失败错误求助
Vite迁移到Rsbuild时内存分配失败错误的排查与解决
问题场景
将现有项目从Vite迁移至Rsbuild,运行rsbuild dev时出现大量重复的内存分配失败错误,进程直接退出,退出代码为1。
配置文件(rsbuild.config.js)
import { defineConfig } from '@rsbuild/core'; import { resolve } from 'path' import {pluginVueJsx} from '@rsbuild/plugin-vue-jsx' import {pluginVue } from '@rsbuild/plugin-vue' export default defineConfig({ plugins: [pluginVue(), pluginVueJsx()], html: { template: './index.html', }, source: { entry: { index: './src/main.js', }, alias: { '@': resolve(__dirname, 'src'), '@cps': resolve(__dirname, 'src/components'), 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js', }, } })
错误信息
> rsbuild dev Rsbuild v1.0.17 ➜ Network: http://172.26.96.1:3000/ ➜ Network: http://192.168.56.1:3000/ ➜ Network: http://192.168.220.1:3000/ ➜ Network: http://192.168.14.1:3000/ ➜ Network: http://192.168.0.17:3000/ ➜ Local: http://localhost:3000/ ➜ press h + enter to show shortcuts start Building... memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of 42memory allocation of 42memory allocation of memory allocation of bytes failed memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of memory allocation of 4242424242424242424242424242424242memory allocation of memory allocation of 424242424242424242424242424242memory allocation of memory allocation of 42memory allocation of memory allocation of bytes failed 4242memory allocation of memory allocation of memory allocation of memory allocation of * 终端进程“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run dev”已终止,退出代码: 1。
问题定位
这个错误的核心原因是**vue-i18n的别名配置触发了无限解析循环**。当配置'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'时,Rsbuild在解析模块时会不断递归解析这个别名指向的文件,最终导致内存耗尽,抛出内存分配失败的错误。
解决方法
1. 移除vue-i18n的别名配置
Rsbuild会自动处理vue-i18n的模块解析逻辑,无需手动指定cjs版本。删除alias中的'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'这一行,修改后的配置如下:
import { defineConfig } from '@rsbuild/core'; import { resolve } from 'path' import {pluginVueJsx} from '@rsbuild/plugin-vue-jsx' import {pluginVue } from '@rsbuild/plugin-vue' export default defineConfig({ plugins: [pluginVue(), pluginVueJsx()], html: { template: './index.html', }, source: { entry: { index: './src/main.js', }, alias: { '@': resolve(__dirname, 'src'), '@cps': resolve(__dirname, 'src/components'), }, } })
2. (可选)升级vue-i18n版本
如果移除别名后仍有vue-i18n相关的兼容问题,建议将vue-i18n升级到适配Vue 3的最新稳定版本(v9.x及以上),确保与Rsbuild的兼容性。
3. (备选)调整Node.js内存限制
如果移除别名后仍存在内存不足的情况,可以在启动命令中增加内存限制参数:
修改package.json中的scripts:
"scripts": { "dev": "node --max-old-space-size=4096 node_modules/@rsbuild/core/bin/rsbuild.js dev" }
4. 验证插件版本一致性
确保@rsbuild/plugin-vue、@rsbuild/plugin-vue-jsx的版本与@rsbuild/core版本(v1.0.17)完全一致,避免版本不匹配导致的异常。
验证
修改配置后,重新运行npm run dev,检查是否能正常启动项目并访问http://localhost:3000/。
内容的提问来源于stack exchange,提问作者ha ha
相关产品推荐
相关产品推荐

