將nodejs從5.1.1升級(jí)到6.10.2之后,執(zhí)行生產(chǎn)環(huán)境的打包(`set NODE_ENV=production && webpack --config webpack.production.config.js`),控制臺(tái)報(bào)錯(cuò):“CALL_AND_RETYR_LAST Allocation failed -javascript heap out of memoty”。
(這是一張圖片,為什么上傳不了?)
我nodejs版本是6.10.2, npm原本是3.10.X,后臺(tái)看在stackoverflow上看到有人說把npm更新到最新版就能解決這個(gè)問題,npm最后更新到4.5.0。
無論是百度還是stackoverflow,我都找過解決方案(我找到了webpack.cmd,在命令行“node "%~dp0\node_modules\webpack\bin\webpack.js" %* ”添加 "-max_old_space_size = 2048"參數(shù)),并且嘗試了,但是沒有用,怎么破?
Hey, I finally did it myself. In fact, now I find that the idea I started to solve the problem is correct, which is to add "--max_old_space_size" to the node command line. The mistake is that I added it in the wrong place (maybe it doesn't work in my environment?). At first, I referred to the solution provided by segmentfault on a problem where the same weback packaging caused nodejs memory overflow, which was to add it to the global webpack.cmd (the path is: C:UsersAdministratorAppDataRoamingnpmwebpack.cmd), as shown below :
But it was found that it did not work. After executing the command, the memory still overflowed. Then I struggled for a long time, and the next day I searched for the answer again on stackoverflow and found this answer:
So, I changed the npm hook script used for packaging in my production environment to: "set NODE_ENV =production && node --max_old_space_size=2048 node_modules/webpack/bin/webpack.js --config webpack.production.config.js". That is to say, I use local/local webpack instead of globally installed webpack for packaging. The memory overflow problem was solved by adding "--max_old_space_size=2048" to nodejs to expand the memory to 2*1024M=2G.