4
votes

I am trying to import the Firebase Admin SDK in my TypeScript (Nest.js) application.

let serviceAccount = require("../../creds.json");
console.log(serviceAccount);

const firebase = require("firebase");
firebase.initializeApp(environment.firebase);

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "projectid"
});

But when I try and build the application I get the following error.

ERROR in ./node_modules/@google-cloud/firestore/src/v1beta1/firestore_client.js
Module not found: Error: Can't resolve './firestore_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/@google-cloud/firestore/src/v1beta1'
 @ ./node_modules/@google-cloud/firestore/src/v1beta1/firestore_client.js 28:17-53
 @ ./node_modules/@google-cloud/firestore/src/v1beta1/index.js
 @ ./node_modules/@google-cloud/firestore/src/index.js
 @ ./src/server/main.server.ts

ERROR in ./node_modules/google-gax/lib/operations_client.js
Module not found: Error: Can't resolve './operations_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax/lib'
 @ ./node_modules/google-gax/lib/operations_client.js 30:17-54
 @ ./node_modules/google-gax/index.js
 @ ./node_modules/@google-cloud/firestore/src/v1beta1/index.js
 @ ./node_modules/@google-cloud/firestore/src/index.js
 @ ./src/server/main.server.ts

ERROR in ./node_modules/google-gax/index.js
Module not found: Error: Can't resolve './package' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax'
 @ ./node_modules/google-gax/index.js 65:18-38
 @ ./node_modules/@google-cloud/firestore/src/v1beta1/index.js
 @ ./node_modules/@google-cloud/firestore/src/index.js
 @ ./src/server/main.server.ts

I have tried to search up any connection between the admin sdk and the google-cloud firestore package but nothing has shown up. I tried to include the firestore library and initialize it as well but this error still shows up.

I included the admin sdk exactly as in the firebase docs with

import * as admin from 'firebase-admin';

after installing with

yarn add firebase-admin

I know the firebase admin sdk can communicate with firestore but unsure of why it would be throwing this error during initialization.

It might have something to do with my project including both a front end and back end component that are compiled together, Node server that serves an angular site. Could the firebase admin sdk be being compiled with the front end possibly causing this error?

Any thoughts?

2

2 Answers

1
votes

Ok I think I may have solved my own problem.

The issue was was that the firebase-admin-sdk was being included in the build process and was causing it to fail.

Fix was: install https://github.com/liady/webpack-node-externals and add that to my webpack.config.ts file and exclude the node_modules folder.

I then had to authenticate through gcloud, instructions here: Could not load the default credentials? (Node.js Google Compute Engine tutorial)

And now the build and serve works.

1
votes

You lost add to webpack json extension for resolve

module.exports = {
    ...
    resolve: {
        extensions: ['.ts', '.js', '.json']
    }
    ...
};