firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.61k stars 363 forks source link

admin.credential.cert Invalid or unexpected token #2276

Open folmosr opened 1 year ago

folmosr commented 1 year ago

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': '-----@----.iam.gserviceaccount.com', }), 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

google-oss-bot commented 1 year ago

I found a few problems with this issue:

lahirumaramba commented 1 year ago

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

lahirumaramba commented 1 year ago

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!

folmosr commented 1 year ago

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

tom-andersen commented 1 year ago

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

folmosr commented 11 months ago

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

folmosr commented 11 months ago

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.

gregfenton commented 6 months ago

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.

iaroslav-ianishyn commented 1 month ago

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 :(

gregfenton commented 1 month ago

@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.)