JavaScript 使用 Azure DevOps 在 Angular 7 中堆内存不足

JavaScript heap out of memory in Angular 7 using Azure DevOps

我收到 致命错误:CALL_AND_RETRY_LAST 分配失败 - JavaScript 堆内存不足 Angular 7.1 升级后。之前的 6.1 版本运行良好。

我只在 Azure DevOps while 运行 ng build --prod 命令中收到错误。它在本地构建得非常好。

下面是详细日志。知道为什么我会收到此错误吗?

2018-11-22T06:40:36.2804965Z ##[section]Starting: npm build
2018-11-22T06:40:36.2809301Z ==============================================================================
2018-11-22T06:40:36.2809365Z Task         : npm
2018-11-22T06:40:36.2809456Z Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Package Management.
2018-11-22T06:40:36.2809496Z Version      : 1.0.27
2018-11-22T06:40:36.2809531Z Author       : Microsoft Corporation
2018-11-22T06:40:36.2809612Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613746)
2018-11-22T06:40:36.2809648Z ==============================================================================
2018-11-22T06:40:36.7158800Z SYSTEMVSSCONNECTION exists true
2018-11-22T06:40:36.8019327Z SYSTEMVSSCONNECTION exists true
2018-11-22T06:40:36.8872807Z [command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files\nodejs\npm.cmd" --version"
2018-11-22T06:40:37.5174051Z 6.4.1
2018-11-22T06:40:38.9461529Z [command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files\nodejs\npm.cmd" config list"
2018-11-22T06:40:39.5927698Z ; cli configs
2018-11-22T06:40:39.5928553Z metrics-registry = "https://registry.npmjs.org/"
2018-11-22T06:40:39.5929006Z scope = ""
2018-11-22T06:40:39.5929176Z user-agent = "npm/6.4.1 node/v8.12.0 win32 x64"
2018-11-22T06:40:39.5929292Z 
2018-11-22T06:40:39.5929425Z ; environment configs
2018-11-22T06:40:39.5929577Z cache = "C:\npm\cache"
2018-11-22T06:40:39.5929756Z prefix = "C:\npm\prefix"
2018-11-22T06:40:39.5929899Z userconfig = "D:\a\1\npm\17729.npmrc"
2018-11-22T06:40:39.5930010Z 
2018-11-22T06:40:39.5930159Z ; builtin config undefined
2018-11-22T06:40:39.5930270Z 
2018-11-22T06:40:39.5930403Z ; node bin location = C:\Program Files\nodejs\node.exe
2018-11-22T06:40:39.5930552Z ; cwd = D:\a\s\client
2018-11-22T06:40:39.5930686Z ; HOME = C:\Users\VssAdministrator
2018-11-22T06:40:39.5930817Z ; "npm config ls -l" to show all defaults.
2018-11-22T06:40:39.5930928Z 
2018-11-22T06:40:39.5931996Z [command]C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files\nodejs\npm.cmd" run build"
2018-11-22T07:33:30.0639548Z FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
2018-11-22T07:33:30.0640628Z  1: node_module_register
2018-11-22T07:33:30.0640676Z  2: v8::internal::FatalProcessOutOfMemory
2018-11-22T07:33:30.0640710Z  3: v8::internal::FatalProcessOutOfMemory
2018-11-22T07:33:30.0640744Z  4: v8::internal::Factory::NewRawTwoByteString
2018-11-22T07:33:30.0640825Z  5: v8::internal::Smi::SmiPrint
2018-11-22T07:33:30.0640861Z  6: v8::internal::StackGuard::HandleInterrupts
2018-11-22T07:33:30.0640894Z  7: v8::internal::AsmJsScanner::IsNumberStart
2018-11-22T07:33:30.0640967Z  8: 000002B496D043C1
2018-11-22T07:33:30.0641001Z npm ERR! code ELIFECYCLE
2018-11-22T07:33:30.0641034Z npm ERR! errno 3
2018-11-22T07:33:30.0641210Z npm ERR! iceux@0.0.1 build: `ng build --prod`
2018-11-22T07:33:30.0641470Z npm ERR! Exit status 3
2018-11-22T07:33:30.0641528Z npm ERR! 
2018-11-22T07:33:30.0641569Z npm ERR! Failed at the iceux@0.0.1 build script.
2018-11-22T07:33:30.0641615Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-11-22T07:33:30.0641703Z 
2018-11-22T07:33:30.0641749Z npm ERR! A complete log of this run can be found in:
2018-11-22T07:33:30.0641795Z npm ERR!     C:\npm\cache\_logs18-11-22T07_33_30_018Z-debug.log
2018-11-22T07:33:30.0641823Z 
2018-11-22T07:33:30.0641910Z > iceux@0.0.1 build D:\a\s\client
2018-11-22T07:33:30.0641951Z > ng build --prod
2018-11-22T07:33:30.0641978Z 
2018-11-22T07:33:30.1213960Z Found npm debug log, make sure the path matches with the one in npm's output: C:\npm\cache\_logs18-11-22T07_33_30_018Z-debug.log
2018-11-22T07:33:30.1214046Z 0 info it worked if it ends with ok
2018-11-22T07:33:30.1214104Z 1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
2018-11-22T07:33:30.1214189Z 1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
2018-11-22T07:33:30.1215590Z 1 verbose cli   'run',
2018-11-22T07:33:30.1215635Z 1 verbose cli   'build' ]
2018-11-22T07:33:30.1215667Z 2 info using npm@6.4.1
2018-11-22T07:33:30.1215727Z 3 info using node@v8.12.0
2018-11-22T07:33:30.1215759Z 4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
2018-11-22T07:33:30.1215801Z 5 info lifecycle iceux@0.0.1~prebuild: iceux@0.0.1
2018-11-22T07:33:30.1215835Z 6 info lifecycle iceux@0.0.1~build: iceux@0.0.1
2018-11-22T07:33:30.1216048Z 7 verbose lifecycle iceux@0.0.1~build: unsafe-perm in lifecycle true
2018-11-22T07:33:30.1216372Z 8 verbose lifecycle iceux@0.0.1~build: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\a\s\client\node_modules\.bin;C:\agents.141.1\externals\git\cmd;C:\tools\mingw64\bin;C:\Program Files\dotnet;C:\mysql-5.7.21-winx64\bin;C:\Program Files\Java\jdk1.8.0_181\bin;C:\npm\prefix;C:\hostedtoolcache\windows\Ruby.5.0\x64\bin;C:\Go1.11\bin;C:\Program Files\Git\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Scripts;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64;C:\Program Files\Microsoft MPI\Bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Chocolatey\bin;C:\Program Files\Docker;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code;C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager;C:\Program Files\Git\cmd;C:\Program Files (x86)\Subversion\bin;C:\Program Files\nodejs\;C:\Program Files\CMake\bin;C:\Users\VssAdministrator\AppData\Local\Microsoft\WindowsApps
2018-11-22T07:33:30.1216877Z 9 verbose lifecycle iceux@0.0.1~build: CWD: D:\a\s\client
2018-11-22T07:33:30.1216979Z 10 silly lifecycle iceux@0.0.1~build: Args: [ '/d /s /c', 'ng build --prod' ]
2018-11-22T07:33:30.1217028Z 11 silly lifecycle iceux@0.0.1~build: Returned: code: 3  signal: null
2018-11-22T07:33:30.1217062Z 12 info lifecycle iceux@0.0.1~build: Failed to exec build script
2018-11-22T07:33:30.1217103Z 13 verbose stack Error: iceux@0.0.1 build: `ng build --prod`
2018-11-22T07:33:30.1217135Z 13 verbose stack Exit status 3
2018-11-22T07:33:30.1217173Z 13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
2018-11-22T07:33:30.1217215Z 13 verbose stack     at emitTwo (events.js:126:13)
2018-11-22T07:33:30.1217250Z 13 verbose stack     at EventEmitter.emit (events.js:214:7)
2018-11-22T07:33:30.1217287Z 13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
2018-11-22T07:33:30.1217332Z 13 verbose stack     at emitTwo (events.js:126:13)
2018-11-22T07:33:30.1217381Z 13 verbose stack     at ChildProcess.emit (events.js:214:7)
2018-11-22T07:33:30.1217413Z 13 verbose stack     at maybeClose (internal/child_process.js:915:16)
2018-11-22T07:33:30.1217448Z 13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
2018-11-22T07:33:30.1217488Z 14 verbose pkgid iceux@0.0.1
2018-11-22T07:33:30.1217521Z 15 verbose cwd D:\a\s\client
2018-11-22T07:33:30.1217551Z 16 verbose Windows_NT 10.0.14393
2018-11-22T07:33:30.1217588Z 17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "build"
2018-11-22T07:33:30.1217629Z 18 verbose node v8.12.0
2018-11-22T07:33:30.1217659Z 19 verbose npm  v6.4.1
2018-11-22T07:33:30.1217690Z 20 error code ELIFECYCLE
2018-11-22T07:33:30.1217727Z 21 error errno 3
2018-11-22T07:33:30.1218070Z 22 error iceux@0.0.1 build: `ng build --prod`
2018-11-22T07:33:30.1218102Z 22 error Exit status 3
2018-11-22T07:33:30.1218134Z 23 error Failed at the iceux@0.0.1 build script.
2018-11-22T07:33:30.1218187Z 23 error This is probably not a problem with npm. There is likely additional logging output above.
2018-11-22T07:33:30.1218220Z 24 verbose exit [ 3, true ]
2018-11-22T07:33:30.1218241Z 
2018-11-22T07:33:30.1286371Z ##[error]Error: Npm failed with return code: 3
2018-11-22T07:33:30.1298403Z ##[section]Finishing: npm build

Package.json

{
  "name": "iceux",
  "version": "0.0.1",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --ssl true",
    "build": "ng build --prod",
    "test": "ng test",
    "testauto": "ng test --watch=false --browsers=ChromeHeadless --code-coverage",
    "lint": "ng lint iceux --format stylish",
    "pree2e": "webdriver-manager update",
    "e2e": "ng e2e --dev-server-target=iceux:serve",
    "e2e:fast": "ng e2e --configuration=fast",
    "e2e:azure": "ng e2e --configuration=azure",
    "e2e:perf": "ng e2e --configuration=perf"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "7.1.0",
    "@angular/common": "7.1.0",
    "@angular/compiler": "7.1.0",
    "@angular/core": "7.1.0",
    "@angular/forms": "7.1.0",
    "@angular/http": "7.1.0",
    "@angular/platform-browser": "7.1.0",
    "@angular/platform-browser-dynamic": "7.1.0",
    "@angular/router": "7.1.0",
    "@epicor/kinetic": "^2.1.0-alpha.20",
    "@ngrx/effects": "^6.1.0",
    "@ngrx/entity": "^6.1.0",
    "@ngrx/store": "^6.1.0",
    "@ngrx/store-devtools": "^6.1.0",
    "@progress/kendo-angular-inputs": "^3.1.3",
    "@progress/kendo-angular-intl": "^1.4.1",
    "@progress/kendo-angular-l10n": "^1.2.0",
    "@progress/kendo-angular-layout": "^3.1.1",
    "@progress/kendo-angular-menu": "^1.0.0",
    "@progress/kendo-angular-toolbar": "^0.2.1",
    "@progress/kendo-angular-treeview": "^2.2.0",
    "@progress/kendo-theme-default": "latest",
    "@telerik/kendo-intl": "^1.4.4",
    "angular2-query-builder": "^0.3.3",
    "core-js": "^2.5.5",
    "json-logic-js": "^1.2.2",
    "lodash": "^4.17.10",
    "logrocket": "^0.6.17",
    "rxjs": "^6.3.3",
    "rxjs-compat": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.10.0",
    "@angular/cli": "7.0.6",
    "@angular/compiler-cli": "7.1.0",
    "@angular/language-service": "7.1.0",
    "@ngrx/schematics": "^6.1.0",
    "@types/jasmine": "^2.8.7",
    "@types/jasminewd2": "~2.0.2",
    "@types/lodash": "^4.14.108",
    "@types/node": "~8.9.4",
    "codelyzer": "^4.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-reporters": "^2.3.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "^1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.4.1",
    "rxjs-tslint": "^0.1.5",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

尝试 运行 像这样构建产品:

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

我昨天遇到了同样的错误,它帮助我避免了它。

在 Mac 上,您可以 运行 export NODE_OPTIONS=--max_old_space_size=8192 在终端中修复未来的 NPM 功能。

尝试 运行 像这样构建产品:

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

如果您使用 angular CLI,您可以使用此命令:

 NODE_OPTIONS="--max-old-space-size=4096" ng serve

修复 JavaScript 堆内存问题的临时解决方案是

ng node --max_old_space_size=4096 && ng build --prod

您还可以添加更多 space

ng node --max_old_space_size=8192 && ng build --prod

或者禁用 AOT 本身

ng build --prod --aot=false --build-optimizer=false

如果您对 运行 使用 ng 服务本身的应用程序有问题,请使用以下命令

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

但我的建议是交叉检查您的应用程序设计并验证所有模块导入,外部库是否正确导入。确保没有重复导入。对所有可重用组件等使用共享模块 More info

我来晚了,但我有针对 Linux 和 Mac

的有效解决方案

要永久修复它,您应该编辑主文件夹中的 .zshrc 文件并将其写在底部

export NODE_OPTIONS=--max_old_space_size=8192

要在 Linux 中修复,您可以在主文件夹中编辑 .bashrc 并在底部写入 [​​=12=]

export NODE_OPTIONS=--max_old_space_size=8192

注意:考虑到您的计算机安装了 8GB (8192MB) 内存,如果您有更多或更少,请相应地调整该值

这可能晚了。只需将您的节点更新到最新版本。主要是少数版本 V 10

的问题

我在 Jenkins 中构建 Angular 8 && Gradle 时遇到了这个问题。我尝试更新 --max_old_space_size 和 NODE_OPTIONS 但它仍然无法正常工作。

我最终添加了 gradle.properties 文件并像这样更新了 JVM 大小:

org.gradle.jvmargs=-Xmx4096m

根据this Windows

的快速有效解决方案
  1. 打开C:\Users\userName\%AppData%\Roaming\npm

  2. Copy/paste下面代码改成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" %* 
    )

假设您使用的是标准 angular cli 工具链(npmng),有一种巧妙的方法可以控制 max_old_space_size 所有 ng 命令。添加到您的 package.json 脚本:

  "scripts": {
    "ng": "node --max_old_space_size=10000 ./node_modules/@angular/cli/bin/ng",
...
  }

然后从命令行你可以使用ng的这个修改版本(不要忘记'run'和'ng'之间的--

npm run -- ng build --prod

如果您决定需要更改堆大小,那真是太好了,您只需在 package.json 中更改一个位置。您所有的命令行用法都保持不变。

(我也更喜欢这种方法,因为它确保调用的ng是您项目指定的本地版本,而不是可能不匹配的全局安装。)

只是补充前面的答案,您可以像这样在 package.json 上设置更大的 space 大小:

"scripts": {
   ...
   "start": "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve",
   ...
},