Skip to content

Best way to deploy TypeScript function to Cloud Function? #254

@dmiranda2791

Description

@dmiranda2791

I am using the TypeScript template to write the function and I faced some issues trying to deploy it using the gcloud functions deploy command.

I tried:

  • The intuitive commando:
gcloud functions deploy pocReadFromBigQueryWriteToFirestore \
--runtime nodejs10 --trigger-http --allow-unauthenticated

Got the error

Deploying function (may take a while - up to 2 minutes)...failed.                                        
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: index.js does not exist; Error ID: e1b2ac36
  • Then, passing a source argument pointing to the build/src directory.
gcloud functions deploy pocReadFromBigQueryWriteToFirestore \
--source=build/src \
--runtime nodejs12 --trigger-http --allow-unauthenticated

Got this error in the cli

Deploying function (may take a while - up to 2 minutes)...failed.                                        
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.

And this one on the functions logs:

2021-02-04T17:47:55.029ZpocReadFromBigQueryWriteToFirestore Provided module can't be loaded.
Default
2021-02-04T17:47:55.029ZpocReadFromBigQueryWriteToFirestore Did you list all required modules in the package.json dependencies?

Next, I tried copying the package.json into the build/src folder and rerun the same command as above.

Deploying function (may take a while - up to 2 minutes)...failed.                                        
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: > protobufjs@6.10.2 postinstall /workspace/node_modules/protobufjs
> node scripts/postinstall


> cloud-function-poc@1.0.0 prepare /workspace
> yarn run compile

yarn run v1.22.4
$ tsc
/bin/sh: 1: tsc: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! cloud-function-poc@1.0.0 prepare: `yarn run compile`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the cloud-function-poc@1.0.0 prepare script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /builder/home/.npm/_logs/2021-02-04T20_07_18_893Z-debug.log; Error ID: beaf8772

Finally, I got it working by copying package.json to build/src/package.json and removing the scripts prepare, pretest and posttest. So, I got to think and I wonder:

  • Why and how this scripts affect the deployment?
  • What is the best strategy to deployment?

Thanks!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions