-
Notifications
You must be signed in to change notification settings - Fork 167
Best way to deploy TypeScript function to Cloud Function? #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, have you looked at other related issues? There are many different configurations depending on how you like to write TS. In general, first build your TS into JS and then you can test the JS with the Functions Framework locally. That will ensure you don't get errors before you deploy. Make sure your build directory isn't ignored and that things work locally. |
@grant thanks for pointing those out. I don't have issues with the development workflow in my local but do you know why the deployment fails when the function is deployed with this package.json? The error shows an execution of the
|
Hi, The main error posted above is: It looks like your function is trying to build the TS program before deploying, so you need typescript to be available. Right now it isn't installed because it's a dev dep. Maybe move it to a dep or be sure to run tsc before deploying to Cloud Functions. |
Hey @grant, the thing is that I want to deploy the files that have already been built by TypeScript, which are in |
@dmiranda2791 OK. Prebuilt JS files are great. This seems like an issue with your function code. In order for me to triage this issue, I need a minimal sample, full steps from start to finish of how you're deploying to GCF. I don't know why https://cloud.google.com/functions/docs/deploying/filesystem Either way, this isn't a Functions Framework issue, unless you see something I'm missing. |
I'm going to close this as the identified error does not pertain to the Functions Framework, rather it is user code. But feel free to keep commenting. As mentioned, clear reproduction steps would help. I've provided a bunch of tips and possible errors in the code in comments above. |
@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 so when deploying a function from the I would like to ask if there was a better approach than this? Ideally the |
Can you provide the list of steps to repro? Would something like this in the
|
@grant thanks for the prompt response! Here are the steps:
Of course, it did not work because the The So should I deploy the pre-compiled code that is inside the |
Thanks! Yes, I see how something is broken in this flow. So I followed those steps, skipping the nodemon part. I then ran the FF locally - that worked. Then I deployed:
And got the error:
It looks like the gts init script compiles in prod, we probably don't want that because we don't want to install TS when building, let's build locally. Remove line:
Deploy:
There's a different error for not finding the function in the root dir. For a workaround, I created a
Seems to work. Just remember to build/watch before deploying. https://us-central1-test-grant.cloudfunctions.net/ash67 I'm not sure why configuring a source in the npm start FF script isn't working in prod, only locally. But this workaround seems pretty lightweight. Hope that helps. |
Thanks so much for providing this workaround. It works like a charm. |
I was starting to test Functions Framework in Typescript and came across the same issue. |
This is very interesting. I had the exact same problem as the OP, but as soon as I removed |
I was having the same issue where my TS compiled field where under
I found out that the {
"name": "cloud_functions_typescript",
"version": "1.0.0",
"description": "Cloud functions typescript",
"main": "build/index.js",
"scripts": { ...}
} |
I hope I am wrong, but there is no way to stop Google Cloud Functions from installing your npm packages for you. I don't want GCF doing CI/CD work. It shouldn't be concerned about my dependencies, they should come packaged in the zip upload to GCF. I understand that GCF doing the install of dependencies fixes the issue with native npm modules that work on your local machine but won't in GCF. Although that is an easy fix in CI/CD by using the right docker image / container to install npm modules inside. For this reason I opted for Cloud Run instead to overcome this issue. If anyone knows a work around do let us know. |
@grant maybe that workaround could be mentioned in the typescript docs that @ash67 referred to. Proposal:
|
@johannesgiani PRs are welcome. The TypeScript doc is a community example doc that I'm sure could be improved / updated. :) |
Unless I'm very confused, the configuration of the scripts in the framework is a bit problematic combined with what runs automatically in Cloud Build (
This is easily observed locally by deleting your node modules and running |
@grant sure let me try that, but for some reason I get @toddbaert did you try just removing the |
Please use a fork and create a PR. I think that error states you've tried to push a branch from a clone, not fork. |
@johannesgiani I can certainly remove the script, but I'm trying to make a larger point that the way the scrips are written is fundamentally flawed. A |
Update typescript readme as proposed in [issue](#254)
Were you ever able to figure this out? @hammadzz |
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:
Got the error
Got this error in the cli
And this one on the functions logs:
Next, I tried copying the package.json into the build/src folder and rerun the same command as above.
Finally, I got it working by copying package.json to build/src/package.json and removing the scripts
prepare
,pretest
andposttest
. So, I got to think and I wonder:Thanks!
The text was updated successfully, but these errors were encountered: