Firebase-admin-node: 14 UNAVAILABLE: Getting metadata from plugin failed with error: fetch is not a function

Created on 24 Apr 2019  路  11Comments  路  Source: firebase/firebase-admin-node

firebase-admin has been changed from 7.2.0 to 7.3.0 and an error is issued with the following code.

__Code__

import * as admin from 'firebase-admin'
const secret = require("./secret.json")
const app = admin.initializeApp({
    credential: admin.credential.cert(secret)
})
describe("sample", () => {
    test("test", async () => {

        await app.firestore().doc("a/c").set({
            "g": "a"
        })

    }, 10000)
    afterAll(() => {
        app.delete()
    })
})

__Error__

    14 UNAVAILABLE: Getting metadata from plugin failed with error: fetch is not a function

      at Object.<anonymous>.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
      at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1204:28)
      at InterceptingListener.Object.<anonymous>.InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
      at InterceptingListener.Object.<anonymous>.InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
      at callback (node_modules/grpc/src/client_interceptors.js:845:24)

  console.error node_modules/grpc/src/common.js:62
    Auth error:TypeError: fetch is not a function

__package.json__

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^7.3.0"
  },
  "devDependencies": {
    "@types/jest": "^24.0.11",
    "@types/node": "^11.13.4",
    "jest": "^24.7.1",
    "ts-jest": "^24.0.2",
    "tslint": "^5.15.0",
    "typescript": "^3.4.3"
  },
  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
  "private": true
}

firestore

Most helpful comment

You can work around this problem by forcing a downgrade to gaxios 1.8.3. If you're using yarn, you can do so by adding an explicit dependency on gaxios and adding a resolutions block, like so:

  "dependencies": {
    "firebase-admin": "^7.3.0",
    "gaxios": "1.8.3"
  },
  "resolutions": {
    "**/gaxios": "1.8.3"
  },

The next release of the packages that depend on gaxios (e.g. google-auth-library) will be updated to gaxios 2.0.1, which includes a fix for this.

All 11 comments

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.

Could you share your package lock file? This seems like it could only happen if you're somehow using an incompatible version of something that the firestore client in the admin SDK depends upon.

Just wanted to let you know I've been able to reproduce what you've seen and I'm now digging deeper. Thanks for the self-contained reproduction.

This issue is an incompatibility between jest and the gaxios project. gaxios is used as a low-level portable plumbing for interacting with Google APIs from JavaScript. Gaxios uses a simple test for whether or not it's running in the browser that fails in this case because jest is defining window.

This is https://github.com/JustinBeckwith/gaxios/issues/87.

I'm not entirely sure exactly which version in which component changed to introduce this incompatibility, but the fix is fairly straightforward. I'll send a PR shortly.

You can work around this problem by forcing a downgrade to gaxios 1.8.3. If you're using yarn, you can do so by adding an explicit dependency on gaxios and adding a resolutions block, like so:

  "dependencies": {
    "firebase-admin": "^7.3.0",
    "gaxios": "1.8.3"
  },
  "resolutions": {
    "**/gaxios": "1.8.3"
  },

The next release of the packages that depend on gaxios (e.g. google-auth-library) will be updated to gaxios 2.0.1, which includes a fix for this.

@wilhuff
Thank you馃ぉ

Hello, @wilhuff
Thanks a lot for the workaround.
I ran into the similar issue and downgrading gaxios to 1.8.3 did fix the issue. But I still have the issue with the latest versions.
Has this recognised as fixed in the latest version of firebase-admin-node and gaxios?

  • "firebase-admin": "7.4.0",
  • "gaxios": "2.0.1",

Here is the overview of my issue and the code.

https://stackoverflow.com/questions/56300385/error-when-testing-firestore-online-with-jest-auth-errortypeerror-fetch-is-n
https://github.com/goodpic/firebase-graphql-boilerplate/blob/master/functions/src/firebase/__tests__/myfirestore.spec.ts

@goodpic I was hitting this issue with firebase-admin 7.3.0, with the latest version 8.1.0 the issue is resolved for me.

@mpalmerlee Thank you for letting me know. The version 8.1.0 did work for me as well!

Upgrading firebase-admin to version 8.1.0 did not fix the issue for me...

But @wilhuff 's solution did!

Was this page helpful?
0 / 5 - 0 ratings