Angular 7-JavaScript堆内存不足错误

问题描述 投票:1回答:1

在Angular 7应用程序中,当我执行ng serve命令时,它抛出“ JavaScript堆内存不足”错误。

从不同的SO答案中,我可以理解,问题是由于对Node的内存分配不足。如他们的回答所述,通过执行以下命令,我可以编译/构建angular应用程序。

node --max_old_space_size=4096 ./node_modules/@angular/cli/bin/ng serve

但是我需要将此内存分配代码写在package.json或一些配置文件中,这样我就不必每次都执行整个命令。还期望代码逻辑应适用于所有环境,并且在较高环境中的构建期间不应引起任何问题。

有人可以帮我解决这个问题。下面是我的package.json代码。

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.config.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
javascript node.js angular angular7
1个回答
0
投票

首次运行:

 npm install -g increase-memory-limit 

然后将这一行添加到您的package.json文件:

"myCustomMemoryComm ": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng"

喜欢此:

"scripts": {
            "ng": "ng",
            "start": "ng serve --proxy-config proxy.config.json",
            "build": "ng build",
            "test": "ng test",
            "lint": "ng lint",
            "e2e": "ng e2e",
            "myCustomMemoryComm": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng"
          },

然后您的下一个命令将每次用于运行项目(服务):

npm run myCustomMemoryComm -serve
© www.soinside.com 2019 - 2024. All rights reserved.