Best way to deploy TypeScript function to Cloud Function?
See original GitHub issueI 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!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:21 (10 by maintainers)
Top Results From Across the Web
Use TypeScript for Cloud Functions - Firebase
With this configuration, a firebase deploy --only functions command builds your TypeScript code and deploys it as functions. Migrating an existing JavaScript ...
Read more >Cloud Functions with TypeScript - Duffblog
Before deploying, it's a good idea to check that it works with npm run gcp-build . To deploy, you'll need to create a...
Read more >Deploying a Google Cloud Function written in typescript
To deploy the function from your source repository, use the below command: gcloud functions deploy NAME \ --source ...
Read more >Developing GCP Cloud Functions locally with Typescript
The best way I've found is to have a single entrypoint function, which then based on the URL path will route through to...
Read more >Deploy a Cloud Function | Cloud Functions Documentation
Deploy from the Google Cloud console inline editor · The left pane lists your source files and allows you to create, rename, and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@grant thanks for the prompt response!
Here are the steps:
build/srcfolder which contained the complied JS code without package.json file.build/srcfolderOf course, it did not work because the
build/srcfolder didn’t contain the package.json which listed all my project dependencies.The
startscript you suggested is already in my project and it works fine locally. It serves my function code at localhost:8080.So should I deploy the pre-compiled code that is inside the
build/srcdirectory or deploy the TS code that would be compiled into JS when the function is being deployed?@grant I am having exactly the same issue as @dmiranda2791.
so when GTS complies typescript code into js and places the compiled JS code into the
build/srcfolder. However, it does not copy package.json over to thebuild/srcfolder. So the possible solution is to manually copy the package.json file and removeprepare, pretest and posttestscripts.so when deploying a function from the
build/srcfolder, it works just fine.I would like to ask if there was a better approach than this? Ideally the
build/srcshould have compiled JS code with package.json file so the whole folder can be deployed.