【问题标题】:Angular FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failedAngular FATAL ERROR:在堆限制附近的无效标记压缩分配失败
【发布时间】:2019-10-29 18:57:13
【问题描述】:

我用过 Angular 5
我试试ng build --prod
显示此错误:
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

我已阅读 thisthis 并将 -max_old_space_size 更改为 package.json 但没有再次生成上述错误。
有什么问题?怎么解决呢?是package.json的问题吗

package.json:

{
  "name": "asd",
  "version": "0.6.2",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "build:dev": "./node_modules/.bin/ng build",
    "build:prod": "./node_modules/.bin/ng build --prod --aot=false",
    "build:aot": "./node_modules/.bin/ng build --prod --aot",
    "build:aot2": "node --max_old_space_size=4096 ./node_modules/.bin/ng build --prod --aot",
    "build": "npm run build:dev",
    "clean:dist": "npm run rimraf -- dist",
    "clean:install": "npm set progress=false && npm install",
    "clean:start": "npm start",
    "clean": "npm cache clean && npm run rimraf -- node_modules doc coverage dist",
    "e2e:live": "npm run e2e -- --elementExplorer",
    "e2e": "npm run protractor",
    "lint": "npm run tslint \"src/**/*.ts\"",
    "prebuild:dev": "npm run clean:dist",
    "prebuild:prod": "npm run clean:dist",
    "preclean:install": "npm run clean",
    "preclean:start": "npm run clean",
    "protractor": "protractor",
    "rimraf": "rimraf",
    "server:dev": "./node_modules/.bin/ng serve",
    "serve": "npm run server:dev",
    "start": "npm run server:dev",
    "test": "./node_modules/.bin/ng test",
    "tslint": "tslint",
    "typedoc": "typedoc",
    "ng": "ng",
    "pree2e": "webdriver-manager update --standalone false --gecko false"
  },
  "private": true,
  ....
}

【问题讨论】:

  • 我的系统内存是16g
  • 我不知道这个错误,但我会将 Angular 升级到版本 6。据我所知,没有人使用版本 5。就像整个社区都跳过了那个版本。

标签: angular


【解决方案1】:

此时升级 Angular 并不能解决问题(但将来可能会)。我的 Angular 7 和 8 项目也可以在我的 Windows 机器上使用它。

看起来您可能需要更新 package.json 中的内存增加。 Here is a link 似乎已经为很多人解决了这个问题,我认为您必须在节点模块中指定 Angular CLI 的路径。

而不是

node --max_old_space_size=4096 ./node_modules/.bin/ng build --prod --aot

试试

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --prod --aot

解决方案中的这条路径看起来是正确的back to Angular 4

【讨论】:

  • 非常感谢,我想编译 Angular 项目。我得到了这个错误。但在您的指导下,问题解决了。节点 --max_old_space_size=4096 node_modules/@angular/cli/bin/ng 服务 --host 0.0.0.0
【解决方案2】:

在 cmd 提示符下运行以下命令:

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

我相信这会解决你的问题。

【讨论】:

    【解决方案3】:

    第一种方式:

    运行方式:

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

    其中 8048 是以 MB 为单位的新内存限制(默认为 ~1500)。或者将命令添加到 package.json 文件中:

    "build-serve": "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve"
    

    并作为 npm run build-serve 运行

    或另一种方式:Windows 的快速有效解决方案

    打开 C:\Users\userName\%AppData%\Roaming\npm

    将以下代码复制/粘贴到您的 ng.cmd:

    @IF EXIST "%~dp0\node.exe" (
        "%~dp0\node.exe" --max_old_space_size=8048 "%~dp0\node_modules\@angular\cli\bin\ng" %* 
    ) ELSE (
        @SETLOCAL 
        @SET PATHEXT=%PATHEXT:;.JS;=;% 
        node --max_old_space_size=8048 "%~dp0\node_modules\@angular\cli\bin\ng" %* 
    )
    

    【讨论】:

      【解决方案4】:

      更新 node.js 到 12 版本帮助我

      【讨论】:

        【解决方案5】:

        export NODE_OPTIONS=--max-old-space-size=8192

        这在 Linux 上帮助了我。我在以下位置找到了这个解决方案: https://support.snyk.io/hc/en-us/articles/360002046418-JavaScript-heap-out-of-memory

        【讨论】:

          【解决方案6】:

          以上没有一个解决方案对我有用。但是,下面的组合解决了这个问题:

          1. 将 Node.js 更新到最新版本(在我的例子中是 v.12.19.0 LTS)
          2. 通过运行以下命令构建增加 JS 堆限制分配的应用程序:node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

          【讨论】:

            【解决方案7】:

            Angular 12 也有同样的问题。在这种情况下运行

            node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration production
            

            帮助。

            【讨论】:

              猜你喜欢
              • 2021-06-10
              • 2021-12-01
              • 1970-01-01
              • 1970-01-01
              • 2020-03-12
              • 2022-10-06
              • 2020-05-14
              • 1970-01-01
              • 2019-10-14
              相关资源
              最近更新 更多