build打包的时候内存超出限制报错:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

package.json:

"scripts": {

"dev": "vite --host",

"build": "vite build && rm -rf ../lbsmart.public/ecdesigner&&mv ./dist ../lbsmart.public/ecdesigner",

},

解决:

su root

全局安装: npm install -g increase-memory-limit

通过环境变量配置increase-memory-limit的最大内存(ps:亲测不起作用,执行increase-memory-limit还是默认的10240。一般默认配置就够用了,若不够可通过字符串批量替换调整max-size):

//set NODE_OPTIONS=--max_old_space_size=8096

//export NODE_OPTIONS="–max-old-space-size=8192"

cd到项目根目录下执行如下命令:

increase-memory-limit

默认是10240.若不够用,则检索node_modules批量替换成更高的数值即可

将--max-old-space-size=10240 替换为 --max-old-space-size=40240 

PS:VSCode默认不检索node_modules,可以开个新窗口将node_modules拖进去,在这个新窗口检索即可   或者  在VSCode Setting中删除search.exclude中的**/node_modules 并 取消勾选use ignore files

若用vscode替换显示权限不足,可执行chmod -R 777 node_modules/.bin/

然后还会报错:Error: Cannot find module '../dist/node --max-old-space-size=10240/cli'

从node_modules文件夹中检索 ../dist/node --max-old-space-size=10240/cli'修改为  require('../dist/node/cli')

然后又会报错: Command failed: /Users/ecmaster/node_modules/esbuild/bin/esbuild --service=0.11.3

执行: node ./node_modules/esbuild/install.js

然后就可以成功打包了!

Logo

前往低代码交流专区

更多推荐