npm异常:FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed

执行npm run build打包vue项目时报这个错,是因为设置的node文件的最大值不够,项目打包后的文件过大导致的。

解决方案:全局安装increase-memory-limit

命令行cmd ,dos窗口运行:cnpm install -g increase-memory-limit,

进入项目文件夹,运行:increase-memory-limit

如果进入项目文件后执行increase-memory-limit后,npm run build报错'"node --max-old-space-size=10240"'不是内部或外部命令,也不是可运行的程序"'

可以在项目根目录下创建以下js文件内容,用node执行该文件,再运行npm run build就可以打包成功了。此处是为了替换node_mosules文件下的.bin文件对应文件内字符串的公共函数。

// 第一次运行项目时通过node执行此脚本
const fs = require('fs')
const path = require('path')
const wfPath =  path.resolve(__dirname, './node_modules/.bin')

fs.readdir(wfPath, (err, files)=>{
   if (err) {
    console.log(err);
   } else {
      if(files.length != 0 ) {
        files.forEach((item)=>{
           if(item.split('.')[1] === 'cmd') {
              replaceStr(`${wfPath}/${item}`, /"%_prog%"/, '%_prog%')
           }
        })
     }
   }
 })

// 参数:[文件路径、 需要修改的字符串、修改后的字符串] (替换对应文件内字符串的公共函数)
function replaceStr(filePath, sourceRegx, targetSrt) {
  fs.readFile(filePath, (err, data)=>{
     if(err) {
     console.log(err)
     } else {
       let str = data.toString();
      str= str.replace(sourceRegx, targetSrt);
       fs.writeFile(filePath, str, (err)=> { console.log(err) })
     }
  })
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值