JavaScript heap out of memory when performing a npm ci within GitLab Pipeline

Viewed 332

I'm running into a heap out of memory error when trying to run an npm ci on a fairly small angular component library. What can I do to get this runner to properly succeed? I tried switching out npm ci with npm install but it seems to run into the same issue.

My .gitlab-ci.yml looks like this:

default:
  image: node:latest

stages:
 - build
 - build-test

cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - .npm/

common-components-build:
  stage: build
  needs: []
  artifacts:
    paths:
      - dist/
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: on_success
  script:
    - npm ci --cache .npm --prefer-offline
    - echo "npm install complete"
    - ng build component-library
  allow_failure: false

common-components-build-test:
  stage: build-test
  needs: ["common-components-build"]
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: on_success
  script:
    - ng test --no-watch --browsers=ChromeHeadless component-library
  allow_failure: false

All these commands work fine locally but after about a minute my runner returns the following error:

$ npm ci --cache .npm --prefer-offline
npm WARN old lockfile 
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile 
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile 
<--- Last few GCs --->
[27:0x6247130]    57317 ms: Scavenge (reduce) 251.7 (258.0) -> 251.7 (259.0) MB, 1.4 / 0.0 ms  (average mu = 0.464, current mu = 0.328) allocation failure; 
[27:0x6247130]    57536 ms: Mark-sweep (reduce) 252.6 (259.0) -> 252.2 (259.7) MB, 216.3 / 0.0 ms  (+ 853.8 ms in 113 steps since start of marking, biggest step 99.1 ms, walltime since start of marking 1206 ms) (average mu = 0.337, current mu = 0.185) all
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb58130 node::Abort() [npm ci]
 2: 0xa7018d node::FatalError(char const*, char const*) [npm ci]
 3: 0xd4665e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [npm ci]
 4: 0xd469d7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [npm ci]
 5: 0xf26735  [npm ci]
 6: 0xf3850d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [npm ci]
 7: 0xf1377a v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm ci]
 8: 0xf14907 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm ci]
 9: 0xef61ab v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [npm ci]
10: 0xeedd24 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [npm ci]
11: 0xeeffd8 v8::internal::FactoryBase<v8::internal::Factory>::NewRawOneByteString(int, v8::internal::AllocationType) [npm ci]
12: 0x102827d v8::internal::JsonParser<unsigned char>::MakeString(v8::internal::JsonString const&, v8::internal::Handle<v8::internal::String>) [npm ci]
13: 0x1029f66 v8::internal::JsonParser<unsigned char>::ParseJsonValue() [npm ci]
14: 0x102a9df v8::internal::JsonParser<unsigned char>::ParseJson() [npm ci]
15: 0xdd0396 v8::internal::Builtin_JsonParse(int, unsigned long*, v8::internal::Isolate*) [npm ci]
16: 0x16e0f39  [npm ci]
/scripts-26421-42548478/step_script: line 148:    27 Killed                  npm ci --cache .npm --prefer-offline
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: command terminated with exit code 1
1 Answers

Determine the version of node are you using to develop locally.

$ which node
/home/username/.asdf/shims/node
$ node --version
v16.15.0

Change the docker image name used in .gitlab-ci.yml to match the node version being used in development. See the "Image Variants" section of hub.docker.com/_/node

default:
  image: node:16.15.0
Related