'AngularFireFunctionsModule from AngularFire Causes build errors

I've run into a bit of a snag when adding in the AngularFireFunctionsModule. For some reason, despite having successfully included AngularFireModule, AngularFirestoreModule and AngularFireAuthModule, when I add anything AngularFireFunctions I get build failures.

Looking at the errors and warnings, the only thing that I can make out of it is that it is trying to find dependencies from the wrong node_modules directory. Which I can't make heads or tails of why it would be doing that.

Here is sample of the error messages while building:

WARNING in ./firebase/functions/node_modules/firebase-functions/lib/config.js 55:23-36
Critical dependency: the request of a dependency is an expression

WARNING in ./firebase/functions/node_modules/firebase-functions/lib/config.js 78:23-36
Critical dependency: the request of a dependency is an expression

WARNING in ./firebase/functions/node_modules/grpc/src/grpc_extension.js 32:12-33
Critical dependency: the request of a dependency is an expression

WARNING in ./firebase/functions/node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js 20:22-48
Critical dependency: the request of a dependency is an expression

WARNING in ./firebase/functions/node_modules/grpc/node_modules/node-pre-gyp/lib/util/versioning.js 17:20-67
Critical dependency: the request of a dependency is an expression

WARNING in ./firebase/functions/node_modules/hash-stream-validation/index.js
Module not found: Error: Can't resolve 'fast-crc32c' in '/Users/name/pwa/firebase/functions/node_modules/hash-stream-validation'

WARNING in ./firebase/functions/node_modules/grpc/node_modules/minimatch/minimatch.js
Module not found: Error: Can't resolve 'path' in '/Users/name/pwa/firebase/functions/node_modules/grpc/node_modules/minimatch'

WARNING in ./firebase/functions/node_modules/write-file-atomic/index.js
Module not found: Error: Can't resolve 'worker_threads' in '/Users/name/pwa/firebase/functions/node_modules/write-file-atomic'

ERROR in ./firebase/functions/node_modules/@google-cloud/firestore/build/src/v1beta1/firestore_client.js
Module not found: Error: Can't resolve './firestore_client_config' in '/Users/name/pwa/firebase/functions/node_modules/@google-cloud/firestore/build/src/v1beta1'
ERROR in ./firebase/functions/node_modules/google-gax/build/src/operationsClient.js
Module not found: Error: Can't resolve './operations_client_config' in '/Users/name/pwa/firebase/functions/node_modules/google-gax/build/src'
ERROR in ./firebase/functions/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/other' in '/Users/name/pwa/firebase/functions/node_modules/mime'
ERROR in ./firebase/functions/node_modules/mime/index.js
Module not found: Error: Can't resolve './types/standard' in '/Users/name/pwa/firebase/functions/node_modules/mime'
ERROR in ./firebase/functions/node_modules/@google-cloud/bigquery/node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in '/Users/name/pwa/firebase/functions/node_modules/@google-cloud/bigquery/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in '/Users/name/pwa/firebase/functions/node_modules/firebase-admin/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in '/Users/name/pwa/firebase/functions/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/google-gax/node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in '/Users/name/pwa/firebase/functions/node_modules/google-gax/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/grpc/node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'child_process' in '/Users/name/pwa/firebase/functions/node_modules/grpc/node_modules/detect-libc/lib'
ERROR in ./firebase/functions/node_modules/graceful-fs/polyfills.js
Module not found: Error: Can't resolve 'constants' in '/Users/name/pwa/firebase/functions/node_modules/graceful-fs'
ERROR in ./firebase/functions/node_modules/@google-cloud/bigquery/node_modules/google-auth-library/build/src/crypto/node/crypto.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/@google-cloud/bigquery/node_modules/google-auth-library/build/src/crypto/node'
ERROR in ./firebase/functions/node_modules/@google-cloud/storage/src/file.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/@google-cloud/storage/src'
ERROR in ./firebase/functions/node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/aws-sign2'
ERROR in ./firebase/functions/node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/aws4'
ERROR in ./firebase/functions/node_modules/crypto-random-string/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/crypto-random-string'
ERROR in ./firebase/functions/node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/ecc-jsbn'
ERROR in ./firebase/functions/node_modules/firebase-admin/lib/auth/token-generator.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/firebase-admin/lib/auth'
ERROR in ./firebase/functions/node_modules/firebase-admin/node_modules/@google-cloud/storage/build/src/file.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/firebase-admin/node_modules/@google-cloud/storage/build/src'
ERROR in ./firebase/functions/node_modules/firebase-admin/node_modules/gcs-resumable-upload/build/src/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/firebase-admin/node_modules/gcs-resumable-upload/build/src'
ERROR in ./firebase/functions/node_modules/firebase-admin/node_modules/google-auth-library/build/src/crypto/node/crypto.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/firebase-admin/node_modules/google-auth-library/build/src/crypto/node'
ERROR in ./firebase/functions/node_modules/gcs-resumable-upload/build/src/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/gcs-resumable-upload/build/src'
ERROR in ./firebase/functions/node_modules/google-auth-library/build/src/pemverifier.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/google-auth-library/build/src'
ERROR in ./firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/google-auth-library/build/src/auth/oauth2client.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/google-auth-library/build/src/auth'
ERROR in ./firebase/functions/node_modules/google-auto-auth/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/google-auto-auth'
ERROR in ./firebase/functions/node_modules/google-gax/node_modules/google-auth-library/build/src/crypto/node/crypto.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/google-gax/node_modules/google-auth-library/build/src/crypto/node'
ERROR in ./firebase/functions/node_modules/hash-stream-validation/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/hash-stream-validation'
ERROR in ./firebase/functions/node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/http-signature/lib'
ERROR in ./firebase/functions/node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/http-signature/lib'
ERROR in ./firebase/functions/node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/jwa'
ERROR in ./firebase/functions/node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/oauth-sign'
ERROR in ./firebase/functions/node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/request/lib'
ERROR in ./firebase/functions/node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/request/lib'
ERROR in ./firebase/functions/node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/request/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'
ERROR in ./firebase/functions/node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in '/Users/name/pwa/firebase/functions/node_modules/sshpk/lib'

The /Users/name/pwa/firebase/functions/node_modules/ directory does exist, however it is not part of the angular application which uses /Users/name/pwa/node_modules/ for node dependencies.

Directory Structure:

pwa
|-> firebase
||-> functions
|||-> distFirebase
|||-> node_modules (cloud functions)
|||-> src
||-> package.json
|-> node_modules (library root)
|-> src
||-> app
|||-> components
|||-> services
|-> package.json

My project's package.json dependancies:

"dependencies": {
    "@angular/animations": "^8.2.14",
    "@angular/common": "^8.2.14",
    "@angular/compiler": "^8.2.14",
    "@angular/core": "^8.2.14",
    "@angular/fire": "^5.2.3",
    "@angular/forms": "^8.2.14",
    "@angular/http": "^7.2.15",
    "@angular/platform-browser": "^8.2.14",
    "@angular/platform-browser-dynamic": "^8.2.14",
    "@angular/pwa": "^0.12.4",
    "@angular/router": "^8.2.14",
    "@angular/service-worker": "^8.2.14",
    "@ngrx/effects": "^8.2.0",
    "@ngrx/store": "^8.2.0",
    "@ngrx/store-devtools": "^8.2.0",
    "@types/json-schema": "^7.0.3",
    "@types/stripe": "^6.32.13",
    "@types/stripe-checkout": "^1.0.3",
    "@types/stripe-v3": "^3.1.9",
    "core-js": "^2.6.10",
    "exif-js": "^2.3.0",
    "firebase": "^5.11.1",
    "rxjs": "^6.5.3",
    "ts-md5": "^1.2.7",
    "tslib": "^1.10.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.19",
    "@angular/cli": "^8.3.19",
    "@angular/compiler-cli": "^8.2.14",
    "@angular/language-service": "^8.2.14",
    "@types/google-libphonenumber": "^7.4.17",
    "@types/jasmine": "^2.8.16",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "electron": "^2.0.18",
    "git-describe": "^4.0.4",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.6",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "rxjs-tslint-rules": "^4.26.3",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~3.5.3"
  }

And my App.Module

imports: [
    CommonModule,
    AppNavigationModule,
    AngularFireModule.initializeApp(environment.firebaseConfig),
    AngularFireAuthModule,
    AngularFirestoreModule,
    AngularFireFunctionsModule, // <-- Errors when added / Builds when removed
    FormsModule,
    ReactiveFormsModule,
  ],

has anyone encountered a similar problem? How was the issue resolved?

Thanks.



Solution 1:[1]

AngularFireFunctions internally imports 'firebase/functions'. Your directory structure makes it so that the typescript compiler resolves 'firebase/functions' to 'pwa/firebase/functions', which causes the error messages during build. Either specify a different baseUrl in tsconfig.json or rename the functions directory.

Solution 2:[2]

I had the exact same issue when I was importing AngularFireFunctionsModule from import { AngularFireFunctionsModule } from '@angular/fire/functions'; into my app.module.ts I changed my firebase folder to _firebase and it fixed the error. Thanks @Pitko. I outputted my error log below so if anyone else runs into the same issue, they will find this thread and hopefully Google's SEO picks up this page.

./firebase/functions/node_modules/hash-stream-validation/index.js:6:8-30 - Warning: Module not found: Error: Can't resolve 'fast-crc32c' in 'D:\Projects\2022-mayocircle-web-ang-firebase\fir
ebase\functions\node_modules\hash-stream-validation'

./firebase/functions/node_modules/write-file-atomic/index.js:34:24-49 - Warning: Module not found: Error: Can't resolve 'worker_threads' in 'D:\Projects\2022-mayocircle-web-ang-firebase\fir
ebase\functions\node_modules\write-file-atomic'

Warning: D:\Projects\2022-mayocircle-web-ang-firebase\node_modules\@angular\fire\functions\es2015\functions.module.js depends on 'firebase/functions'. CommonJS or AMD dependencies can cause
 optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: D:\Projects\2022-mayocircle-web-ang-firebase\node_modules\firebase\firestore\dist\index.esm.js depends on '@firebase/firestore'. CommonJS or AMD dependencies can cause optimization
 bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: D:\Projects\2022-mayocircle-web-ang-firebase\src\app\app.module.ts depends on 'angularfire2'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: D:\Projects\2022-mayocircle-web-ang-firebase\src\app\app.module.ts depends on 'angularfire2/auth'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: D:\Projects\2022-mayocircle-web-ang-firebase\src\app\app.module.ts depends on 'angularfire2/firestore'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies



./firebase/functions/node_modules/@google-cloud/common/build/src/util.js:52:15-32 - Error: Module not found: Error: Can't resolve 'stream' in 'D:\Projects\2022-mayocircle-web-ang-firebase\f
irebase\functions\node_modules\@google-cloud\common\build\src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

./firebase/functions/node_modules/@google-cloud/firestore/build/src/bulk-writer.js:21:13-30 - Error: Module not found: Error: Can't resolve 'assert' in 'D:\Projects\2022-mayocircle-web-ang-
firebase\firebase\functions\node_modules\@google-cloud\firestore\build\src' ..
If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "assert": require.resolve("assert/") }'
        - install 'assert'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "assert": false }



** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **


× Failed to compile.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pitko
Solution 2 Quentin Mayo