Firebase-tools: Firebase Emulator Error: Channel credentials must be a ChannelCredentials object

Created on 5 May 2020  ยท  7Comments  ยท  Source: firebase/firebase-tools

[REQUIRED] Environment info

firebase-tools: 8.2.0
Platform: macOS

[REQUIRED] Test case

index.js
````
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const DEBUG = true;

function markUserUpdated(userId) {
return new Promise((resolve, reject) => {
admin.firestore().doc(users/${userId}).update({ dbUpdated: new Date() })
.then(() => {
if (DEBUG) console.log('User successfully marked Updated', userId);
resolve();
})
.catch(e => reject(e));
});
}

exports.writeContact = functions.firestore
.document('users/{userId}/contacts/{contactId}')
.onWrite((doc, context) => {
const { userId } = context.params;
return markUserUpdated(userId);
});
````

[REQUIRED] Steps to reproduce

Run the emulator (functions & firestore) with this code.
Modify a contact in firestore.

[REQUIRED] Expected behavior

The users/${userId} doc should be updated and you should see a User successfully marked Updated in the console.

[REQUIRED] Actual behavior

i functions: Beginning execution of "writeContact" โš  functions: TypeError: Channel credentials must be a ChannelCredentials object at new ChannelImplementation (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/channel.js:65:19) at new Client (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/client.js:57:36) at new ServiceClientImpl (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/make-client.js:49:5) at GrpcClient.createStub (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/build/src/grpc.js:220:22) at processTicksAndRejections (internal/process/task_queues.js:97:5) โš  Your function was killed because it raised an unhandled error.

Additional notes

If I replace the function markUserUpdated by the following one, then the error is not thrown.

function markUserUpdated(userId) {
    return Promise.resolve();
}

Note also that the datas are perfectly loaded from Firestore into the app, so the Firestore Emulator is running normallly.

Attention emulator-suite

Most helpful comment

OK, I've solved it myself.
Just update firebase-admin to 9.0.0

npm install firebase-admin@latest

All 7 comments

@Pitouli could you show me the dependencies in your package.json inside your functions directory?

Sure!

Interesting point: I clone my last commit (which I'm 100% sure it worked well) and started to reintegrate one by one my modifications.

Currently, I'm at a point where all the src files in the functions directory of my copy project are identical to those of the cloned project, and I have the bug in the cloned but not in the copy :/

I continue to explore what could be the source of the issue.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "main": "dist/index.js",
  "scripts": {
    "lint": "eslint .",
    "build": "babel 'src' --out-dir 'dist' --source-maps",
    "watch": "npm run build --watch",
    "serve": "npm run watch && firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "predeploy": "npm run build",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "@google-cloud/storage": "^4.7.0",
    "firebase-admin": "^8.11.0",
    "firebase-functions": "^3.6.1",
    "ical-generator": "^1.10.0",
    "moment": "^2.25.3",
    "object-hash": "^2.0.3"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.6",
    "@babel/preset-env": "^7.9.6",
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.2.1",
    "firebase-functions-test": "^0.1.7"
  },
  "private": true,
  "jshintConfig": {
    "esversion": 6
  }
}

Ok, since it was working on my duplicated project, I decided to remove all the nodes_modules from my main one and reinstall them fresh.

Now, it works.

I do not understand why this error happened, but since it's a dev tool, I would say that it is not so important if the error is rare and the solution easy. So I close this issue.

@Pitouli glad you fixed it! This error can happen when you have multiple versions of gRPC libraries in your node_modules. gRPC has a C++ implementation and sometimes passing objects (like Credentials) between versions does not work.

The good news is that almost all of Google's Node.js libraries have moved to grpc-js which is a pure JS implementation so eventually this kind of error should disappear.

Sorry @samtstern I am still having this issue. I remove all node_modules and installing all of them again.

Project structure:

.
โ””โ”€โ”€ functions
    โ”œโ”€โ”€ packages
    โ”‚ย ย  โ””โ”€โ”€ core
    โ”‚ย ย      โ”œโ”€โ”€ controllers
    โ”‚ย ย      โ”œโ”€โ”€ services
    โ”‚ย ย      โ””โ”€โ”€ tests
    โ”‚ย ย          โ”œโ”€โ”€ controllers
    โ”‚ย ย          โ””โ”€โ”€ services
    โ”œโ”€โ”€ companies
    โ”‚ย ย  โ”œโ”€โ”€ api
    โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ controllers
    โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ v1
    โ”‚ย ย  โ”‚ย ย          โ”œโ”€โ”€ authentication
    โ”‚ย ย  โ”‚ย ย          โ”œโ”€โ”€ healthcheck
    โ”‚ย ย  โ”‚ย ย          โ”œโ”€โ”€ image
    โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ roles
    โ”‚ย ย  โ”œโ”€โ”€ providers
    โ”‚ย ย  โ”œโ”€โ”€ services
    โ”‚ย ย  โ”œโ”€โ”€ services-config
    โ”‚ย ย  โ””โ”€โ”€ tests
    โ”‚ย ย      โ”œโ”€โ”€ api
    โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ controllers
    โ”‚ย ย      โ”‚ย ย      โ””โ”€โ”€ v1
    โ”‚ย ย      โ”‚ย ย          โ”œโ”€โ”€ authentication
    โ”‚ย ย      โ”‚ย ย          โ””โ”€โ”€ roles
    โ”‚ย ย      โ”œโ”€โ”€ fixtures
    โ”‚ย ย      โ””โ”€โ”€ services
    โ”œโ”€โ”€ test
    โ””โ”€โ”€ user-management
        โ”œโ”€โ”€ controllers
        โ”‚ย ย  โ””โ”€โ”€ v1
        โ”œโ”€โ”€ providers
        โ”œโ”€โ”€ services
        โ”œโ”€โ”€ services-config
        โ””โ”€โ”€ tests
            โ”œโ”€โ”€ controllers
            โ”‚ย ย  โ””โ”€โ”€ v1
            โ””โ”€โ”€ services

This is my package.json of the functions that triggers the error.

{
  "name": "user-management",
  "version": "1.0.0",
  "description": "User Management,
  "scripts": {
    "generate:keys:dev": "cp services-config/app.prod.json services-config/app.json",
    "generate:keys:test": "cp services-config/app.test.json services-config/app.json",
    "dev": "npm run generate:keys:dev && nodemon index.js",
    "start": "npm run generate:keys:dev && node index.js",
    "test": "npm run generate:keys:test && nyc --reporter=lcov ava",
    "watch:test": "ava --watch",
    "lint": "npx eslint ."
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "engines": {
    "node": "10"
  },
  "private": true,
  "devDependencies": {
    "@ava/babel": "^1.0.1",
    "ava": "^3.6.0",
    "eslint": "^6.8.0",
    "eslint-plugin-promise": "^4.2.1",
    "husky": "^4.2.5",
    "mock-req-res": "^1.1.4",
    "nodemon": "^2.0.3",
    "nyc": "^15.0.1",
    "proxyquire": "^2.1.3",
    "sinon": "^9.0.2"
  },
  "dependencies": {
    "@company/core": "file:../packages/core",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-multipart-file-parser": "^0.1.2",
    "firebase": "^7.14.0",
    "firebase-admin": "^8.10.0",
    "json2csv": "^5.0.0",
    "morgan": "^1.10.0",
    "uuid": "^7.0.3",
    "xhr2": "^0.2.0"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint && npm run test"
    }
  }
}

Please, could you help me to solve this issue.

I got the same error in cypress end-to-end script, when access to firestore.
Removing node_modules and npm install again not help either.
Here the stack trace

   at <unknown> (http://localhost:4200/__cypress/runner/cypress_runner.js:158370:20)
From previous event:
    at Context.task (http://localhost:4200/__cypress/runner/cypress_runner.js:158352:19)
From Your Spec Code:
    at Context.eval (http://localhost:4200/__cypress/tests?p=src/support/index.ts:190691:20)
From Node.js Internals:
    TypeError: Channel credentials must be a ChannelCredentials object
      at new ChannelImplementation (/home/yglin/Projects/ygg/node_modules/google-gax/node_modules/@grpc/grpc-js/src/channel.ts:150:14)
      at new Client (/home/yglin/Projects/ygg/node_modules/google-gax/node_modules/@grpc/grpc-js/src/client.ts:146:31)
      at new ServiceClientImpl (/home/yglin/Projects/ygg/node_modules/google-gax/node_modules/@grpc/grpc-js/src/make-client.ts:119:4)
      at GrpcClient.createStub (/home/yglin/Projects/ygg/node_modules/google-gax/src/grpc.ts:290:19)
    Caused by: Error: 
      at Firestore.getAll (/home/yglin/Projects/ygg/node_modules/@google-cloud/firestore/build/src/index.js:683:24)
      at DocumentReference.get (/home/yglin/Projects/ygg/node_modules/@google-cloud/firestore/build/src/reference.js:199:33)
      at Object.callFirestore (/home/yglin/Projects/ygg/node_modules/cypress-firebase/lib/tasks.js:140:15)
      at tasksWithFirebase.<computed> (/home/yglin/Projects/ygg/node_modules/cypress-firebase/lib/plugin.js:27:36)
      at invoke (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/child/task.js:41:15)
      at <unknown> (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/util.js:41:15)
      at tryCatcher (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:24)
      at Function.Promise.attempt.Promise.try (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/method.js:39:30)
      at Object.wrapChildPromise (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/util.js:40:24)
      at Object.wrap (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/child/task.js:47:9)
      at execute (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:142:13)
      at EventEmitter.<anonymous> (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:235:6)
      at EventEmitter.emit (events.js:210:6)
      at process.<anonymous> (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/lib/plugins/util.js:19:23)
      at process.emit (events.js:210:6)
      at process.emit (/home/yglin/.cache/Cypress/4.11.0/Cypress/resources/app/packages/server/node_modules/source-map-support/source-map-support.js:495:22)
      at emit (internal/child_process.js:876:13)
      at processTicksAndRejections (internal/process/task_queues.js:81:22)

Does anyone got an alternative solution for this ?

OK, I've solved it myself.
Just update firebase-admin to 9.0.0

npm install firebase-admin@latest

Was this page helpful?
0 / 5 - 0 ratings