question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to deploy TypeScript function to Cloud Function?

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
ash67commented, Apr 28, 2021

@grant thanks for the prompt response!

Here are the steps:

  1. I followed the steps mentioned here to create a project, write code in TS, run & test code locally.
  2. My project had dependencies of other NPM modules so I added them in the package.json file
  3. GTS created the build/src folder which contained the complied JS code without package.json file.
  4. I deployed my function code from the build/src folder

Of course, it did not work because the build/src folder didn’t contain the package.json which listed all my project dependencies.

The start script 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/src directory or deploy the TS code that would be compiled into JS when the function is being deployed?

2reactions
ash67commented, Apr 28, 2021

@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/src folder. However, it does not copy package.json over to the build/src folder. So the possible solution is to manually copy the package.json file and remove prepare, pretest and posttest scripts.

so when deploying a function from the build/src folder, it works just fine.

I would like to ask if there was a better approach than this? Ideally the build/src should have compiled JS code with package.json file so the whole folder can be deployed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found