npm内存溢出

项目过大运行项目内存溢出

报错代码

运行内存溢出 increase-memory-limit ‘“node --max-old-space-size=8192”’
不是内部或外部命令,也不是可运行的程序

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation
failed - JavaScript heap out of memory
‘cross-env‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件

解决方案

1.第一步:安装
使用 increase-memory-limit

npm install --save-dev increase-memory-limit
npm install --save-dev cross-env

安装完成插件,在package.json scripts中添加下面代码,并运行

"limit": "cross-env LIMIT=8192 increase-memory-limit",

2.第二步:替换 “%_prog%”
如果报错 “不是内部或外部命令” 在根目录下 新建与node-modules同级的文件,increase-memory-limit.js,把wfpath改成自己的项目地址即可,注意有两处地方需要调整

const fs = require('fs')
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) => {
                if (err) console.log(err);
            })
        }
    })
}
var wfPath = 'D:/mgsworker/newTran/node_modules/.bin'
fs.readdir(wfPath, (err, files) => {
    if (err) console.log(err);
    else {
        if (files.length != 0) {
            files.forEach((item) => {
                var wfPath = 'D:/mgsworker/newTran/node_modules/.bin';// 或者var wfPath =  path.resolve(__dirname, '../node_modules/.bin')
                if (item.split('.')[1] === 'cmd') {
                    wfPath += `/${item}`;
                    replaceStr(wfPath, /"%_prog%"/, '%_prog%')
                }
            })
        }
    }
})

在这里插入图片描述

3.第三步:运行替换:
文件根目录下执行命令:node increase-memory-limit.js 执行完可以删除脚本文件

4.第四步:正常运行和打包了

补充

当npm install 运行报错的时候 多半是源的问题;
因为访问https://registry.npmjs.org这个地址需要翻墙才能访问,所以在国内经常会出现连接超时等问题。为了解决这个问题,可以将npm的源设置为国内的镜像例如淘宝镜像。

npm config set registry https://registry.npmmirror.com
npm config get registry // 查看设置

npm 官方原始镜像网址是:https://registry.npmjs.org/
淘宝最新 NPM 镜像https://registry.npmmirror.com
阿里云 NPM 镜像: https://npm.aliyun.com
腾讯云 NPM 镜像:https://mirrors.cloud.tencent.com/npm/
华为云 NPM 镜像:https://mirrors.huaweicloud.com/repository/npm/
网易 NPM 镜像:https://mirrors.163.com/npm/
中科院大学开源镜像站:http://mirrors.ustc.edu.cn/
清华大学开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值