Search code examples
node.jsnpmgoogle-cloud-platformgoogle-cloud-functionsgoogle-codelab

Google chat codelabs error when deploy (OperationError: code=3)


I followed official google chat codelabs, but in the last step when I run gcloud functions deploy app --trigger-http --security-level=secure-always, I got anerror:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: npm ERR! Cannot read property 'googleapis' of undefined

I did follow all instruction, work fine until I run npm install --save googleapis and require that library on my code.

here my error code:

const { buildConfigurationForm, MAX_NUM_OF_OPTIONS } = require('./config-form');
const { buildVoteCard } = require('./vote-card');
const {google} = require('googleapis');

googleapis is already on package.json and also package-lock.json.

here my package.json contains:

{
  "name": "poll-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "googleapis": "^110.0.0"
  }
}

but why I still fail ? I just follow the tutorial from the official codelabs by google?

if I remove googleapis using command npm remove googleapis it will return another error:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

Only after I remove const {google} = require('googleapis'); and all related code, it can successfully deployed. But I want to use googleapis library as the codelabs instruction.


Solution

  • That happened because of a different node version. My local(GCP CLI console) is node 18, but the google cloud function runtime is 14.

    My solution is to remove package-lock.json.

    But I think it's better to use the same version between CLI and GCF.