使用Azure DevOps时,Angular 7中出现JavaScript堆内存不足的问题

33

在升级到Angular 7.1之后,我遇到了FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory错误。之前的版本6.1工作得非常好。

只有在Azure DevOps运行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\1\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\_logs\2018-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\1\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\_logs\2018-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\1\s\client\node_modules\.bin;C:\agents\2.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\2.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 Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\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\1\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\1\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

包描述文件.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"
  }
}

还有webpack及其配置文件。Webpack和中间件的版本是多少? - Niladri
你可以分享一下 package.json 文件吗?我之前也遇到了同样的问题,昨天已经解决了。 - Niladri
我已经添加了package.json文件。除了Angular使用的配置外,我没有使用任何额外的webpack配置。 - Abhishek
为什么构建一个简单的Angular应用程序需要8GB? - JRichardsz
15个回答

38
尝试以这种方式运行生产构建:
node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod

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


1
然而在我的情况下,--max_old_space_size=4096 是可行的。 - Niladri
使用--max_old_space_size=5048后,没有显示任何aot构建错误。它像普通的JIT编译器一样构建,并发出了一个巨大的main.chunk.js文件夹。 - Arron
我使用了以下命令:"prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod --aot --build-optimizer=false --verbose=true",但没有用。 - Arron
@Arron,为什么你禁用了构建优化器? - iliya.rudberg
2
增加堆限制并不是解决方案,这只是一个补丁。我需要一种可以减少内存使用的解决方案。我使用 siege 和 node inspector 调试了 Angular7 应用程序,似乎每次 Angular 对象和数组被推入内存时都没有释放内存。 - Krrish Yadav
显示剩余10条评论

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

7

解决JavaScript堆内存问题的临时方法是:

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

你可以添加更多的空间。
ng node --max_old_space_size=8192 && ng build --prod

或者禁用AOT本身

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

如果您在使用“ng serve”运行应用程序方面遇到问题,则请使用以下命令:
node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve

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


4

我回答有点晚,但我有适用于Linux和Mac的工作解决方案

要永久解决它,您应该编辑您的主目录中的.zshrc文件,并在底部写入以下内容

export NODE_OPTIONS=--max_old_space_size=8192

要在Linux中解决此问题,您可以编辑主目录中的.bashrc并在底部写入以下内容

export NODE_OPTIONS=--max_old_space_size=8192

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


2
假设您正在使用标准的Angular CLI工具链(npmng),有一种巧妙的方法可以控制所有ng命令的max_old_space_size。将以下内容添加到您的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是项目指定的本地版本,而不是全局安装版本,这可能不匹配。)

这个能在Angular 7(和Node 14)上运行吗? - undefined

2
如果您正在使用Angular CLI,可以使用以下命令:
 NODE_OPTIONS="--max-old-space-size=4096" ng serve

0

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

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

org.gradle.jvmargs=-Xmx4096m

0

仅补充之前的答案,您可以在package.json中设置更大的空间大小,如下所示:

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

0
可能有点晚了。只需将您的Node更新到最新版本。主要问题出现在几个版本 V 10 中。

0

根据this,这是Windows的快速有效解决方案

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

  2. 将以下代码复制/粘贴到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" %* 
    )

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接