firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

admin.credential.cert Invalid or unexpected token

Open folmosr opened this issue 2 years ago • 10 comments

I took this package to the latest version 11.10.1, I always use this variation to connect to the database

admin.initializeApp({ credential: admin.credential.cert({ 'projectId': '----', 'privateKey': '-----BEGIN PRIVATE KEY-----\n....\n-----END PRIVATE KEY-----\n', 'clientEmail': '[email protected]', }), databaseURL: https://pickapp-cl-qa-279013.firebaseio.com, });

but now It's throwing this exception

const initialSettings = this.firestoreSettings.get(databaseId) ?? {}; SyntaxError: Invalid or unexpected token at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)

I have tested this from version 11.3.0 and below and it works as expected

folmosr avatar Aug 07 '23 21:08 folmosr

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Aug 07 '23 21:08 google-oss-bot

Hey @tom-andersen this could be related to the multidb feature. Could you take a look when you have a moment? Thanks!

lahirumaramba avatar Aug 11 '23 18:08 lahirumaramba

Hey @folmosr what version of @google-cloud/firestore dependency are you using in your project? Could you try upgrading @google-cloud/firestore to 6.7.0 and see if that solves the issue? Thanks!

lahirumaramba avatar Aug 22 '23 14:08 lahirumaramba

Hey @lahirumaramba , I'm using these 2 packages

@firebase/firestore 4.0.0, firebase-admin 11.0.0

I've taken @firebase/firestore to version 4.1.1 but I'm still having the same issue

folmosr avatar Aug 22 '23 15:08 folmosr

Hey @lahirumaramba. The problem looks to be related to parsing the following line:

const initialSettings = this.firestoreSettings.get(databaseId) ?? {};

This might have something to do with the version of NodeJS. the ``??` operator requires v14 or higher.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing#browser_compatibility

tom-andersen avatar Aug 28 '23 21:08 tom-andersen

In my Docker file I'm using node:18-alpine and locally Node v16.20.0

folmosr avatar Sep 05 '23 14:09 folmosr

Hey @folmosr what version of @google-cloud/firestore dependency are you using in your project? Could you try upgrading @google-cloud/firestore to 6.7.0 and see if that solves the issue? Thanks!

Hey @lahirumaramba I tested this packed but I ended up getting the same result, now I can see there is a new version of firebase-admin, I'll test this version and I will let you know how it goes, thanks.

folmosr avatar Sep 29 '23 16:09 folmosr

I am having the same issue when I go to use any Admin SDK function after initializing with a service account. Code works fine with v10.3.0 but fails with all v11 and v12 that I have tried.

The same issue is detailed in this SO post, including my comments from today.

With v11.11.1 I get this exception:

/Users/greg/work/prod/tick8s/node_modules/firebase-admin/lib/auth/base-auth.js:421
        if (properties?.providerToLink) {
                       ^

SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

Note I am running my code as ESM yet I see the error above mentions /cjs/

UPDATE

This seems to be an ESM vs CJS issue. My code runs as ESM (it is an older codebase using node -r esm ...) and the stack trace here and in the OP indicates node:internal/modules/cjs/loader.

Trying to build a repro is proving challenging....the issue goes away with a clean codebase and nice/proper handling of ESM. The code works fine with v10, v11 and v12 in this configuration.

gregfenton avatar Feb 23 '24 23:02 gregfenton

I am having the same issue when I go to use any Admin SDK function after initializing with a service account. Code works fine with v10.3.0 but fails with all v11 and v12 that I have tried.

The same issue is detailed in this SO post, including my comments from today.

With v11.11.1 I get this exception:

/Users/greg/work/prod/tick8s/node_modules/firebase-admin/lib/auth/base-auth.js:421
        if (properties?.providerToLink) {
                       ^

SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

Note I am running my code as ESM yet I see the error above mentions /cjs/

UPDATE

This seems to be an ESM vs CJS issue. My code runs as ESM (it is an older codebase using node -r esm ...) and the stack trace here and in the OP indicates node:internal/modules/cjs/loader.

Trying to build a repro is proving challenging....the issue goes away with a clean codebase and nice/proper handling of ESM. The code works fine with v10, v11 and v12 in this configuration.

Hello! I have absolutely the same issue. Could you please give more details on "the issue goes away with a clean codebase and nice/proper handling of ESM". I can't understand where to start to dig at all. Downgrading firebase-admin version is not an option now :(

iaroslav-ianishyn avatar Jul 08 '24 11:07 iaroslav-ianishyn

@iaroslav-ianishyn by "ESM" I mean that my code base is now using "type: module" in package.json and all my code uses ESM syntax (e.g. import instead of require(), etc.)

gregfenton avatar Jul 08 '24 13:07 gregfenton