javascriptangularangular-cliangular-cli-v8angular-cli-v9

configuring custom path for ngcc in angular project


I am having a requirement where node_modules required for a project are already installed and stored somewhere. And we are running multiple instances of the angular project each inside a separate docker container and we mount the node_modules folder to these containers.

So basically same node_modules folder is shared among all these running containers.

Everything works fine, but when we run testcases -> npm run test (jest --json --outputFile=./testOutput.json; node transformJestOutputToMochaStyle.js) its failing with the below error, which is obvious as we are mounting the node_modules folder as read only.

My Question is is there any way we can specify any custom directory which can be used by ngcc for creating the related temp files like - .ngcc_lock_file etc. ?

gxjc5w@84d7538af516:~/project/application$ npm run test

> angular-interview@0.0.0 test
> jest --json --outputFile=./testOutput.json; node transformJestOutputToMochaStyle.js

Determining test suites to run...
ngcc-jest-processor: running ngcc
Error: EROFS: read-only file system, open '/deps/node_modules/.ngcc_lock_file'
    at Object.openSync (node:fs:585:3)
    at Module.writeFileSync (node:fs:2157:35)
    at NodeJSFileSystem.writeFile (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-TOKOIIBI.js:357:9)
    at LockFileWithChildProcess.write (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-2LU77VSE.js:1730:13)
    at AsyncLocker.create (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-2LU77VSE.js:1682:30)
    at AsyncLocker.lock (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-2LU77VSE.js:1671:16)
    at ClusterExecutor.execute (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-2LU77VSE.js:1566:26)
    at mainNgcc (file:///deps/node_modules/@angular/compiler-cli/bundles/chunk-2LU77VSE.js:2113:19)
    at file:///deps/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js:32:11
    at file:///deps/node_modules/@angular/compiler-cli/bundles/ngcc/main-ngcc.js:42:3
Error: Jest: Got error running globalSetup - /deps/node_modules/jest-preset-angular/global-setup.js, reason:  NGCC failed .
    at runNgccJestProcessor (/deps/node_modules/jest-preset-angular/build/utils/ngcc-jest-processor.js:42:19)
    at /deps/node_modules/jest-preset-angular/build/config/global-setup.js:10:56
    at Generator.next (<anonymous>)
    at /deps/node_modules/tslib/tslib.js:118:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/deps/node_modules/tslib/tslib.js:114:16)
    at globalSetup (/deps/node_modules/jest-preset-angular/build/config/global-setup.js:6:35)
    at /deps/node_modules/@jest/core/build/runGlobalHook.js:125:19
    at ScriptTransformer.requireAndTranspileModule (/deps/node_modules/@jest/transform/build/ScriptTransformer.js:905:24)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
node:internal/fs/utils:344
    throw err;
    ^

Error: ENOENT: no such file or directory, open './testOutput.json'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at readTestOutput (/candidate_files/preview/gxjc5w/editablequestions-angular7-q1675409290800/application/transformJestOutputToMochaStyle.js:41:28)
    at Object.<anonymous> (/candidate_files/preview/gxjc5w/editablequestions-angular7-q1675409290800/application/transformJestOutputToMochaStyle.js:50:20)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: './testOutput.json'
}

I tried looking for it, could not find anything.


Solution

  • ngcc needs to write the updated packages into the node_modules tree, so just storing the lockfile elsewhere won't help. The CLI won't run ngcc when a readonly filesystem is detected.

    Since v12.0.0, jest-preset-angular provide a possibility to skip ngcc via globalThis by doing the following -

    globalThis.ngJest = {
      skipNgcc: true,
      tsconfig: 'tsconfig.spec.json', // this is the project root tsconfig
    };
    
    module.exports = {
      //...
      globalSetup: 'jest-preset-angular/global-setup',
    };
    

    For details check this - https://thymikee.github.io/jest-preset-angular/docs/guides/angular-ivy/#control-ngcc-processing