npm內存溢出的解決方案


當前端項目過大時,運行的時候就會碰到內存溢出的問題

這個時候需要全局安裝 increase-memory-limit插件

執行 npm install -g  increase-memory-limit

在package.json的同級目錄下輸入increase-memory-limit   運行這個插件:

 然后重啟項目。

如果重啟的時候出現報錯:

 那這個時候就麻煩一點了,需要修改node_modules下的.bin文件中的所有.cmd文件,將里面的"%_prog%"  去掉雙引號 改成  %_prog%

原生vscode不支持對.cmd文件的全局修改,下面提供一種使用node對指定文件進行全局修改的解決方案:
這串代碼需要在node環境下執行,需要引入fs模塊(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);
             })
         }
     })
 }
 
這里我使用了絕對路徑,如果你的項目build文件與node_modules文件在同級目錄,並且你打算將下面的代碼放在build目錄下的webpack.*.config.js文件里面去執行這段代碼 (執一次就可以刪掉)。
(引入path模塊后),那么你的路徑也可以寫成:var wfPath = path.resolve(__dirname, '../node_modules/.bin');
 
讀取文件、修改文件
var wfPath = 'D:/***/***/node_modules/.bin';
fs.readdir(wfPath, (err, files)=>{
   if(err) console.log(err);
      else {
         if(files.length != 0 ) {
             files.forEach((item)=>{
                 var wfPath = 'D:/***/***/node_modules/.bin';// 或者var wfPath =  path.resolve(__dirname, '../node_modules/.bin')
                 if(item.split('.')[1] === 'cmd') {
                    wfPath += `/${item}`;
                    replaceStr(wfPath, /"%_prog%"/, '%_prog%')
                 }
             })
         }
     }
 })
直接執行這個文件 node 文件名
或者進入到build目錄下執行這個文件,node  webpack.*.config.js  //將這段代碼放在webpack.*.config.js的情況
隨后就可以重啟項目了。
 
本人初級程序員一枚~
歡迎大家進群交流:
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM