I'm following a cloud functions tutorial and I'm trying to deploy cloud functions using firebase deploy -- functions only, and it worked the first time, but I realized I forgot to save the index.js file so no functions were deployed. When I saved and tried again it threw me a node error:
My functions look like this:
Related
I have a Node server and multiple controllers that perform DB operations and helpers (For e-mail, for example) within that directory.
I'd like to use source from that directory within my functions. Assuming the following directory structure:
src/
server/
/app/controllers/email_helper.js
fns/
send-confirm/
What's the best way to use email_helper within the send-confirm function?
I've tried:
Symbolically linking the 'server' directory
Adding a local repo to send-confirm/package.json
Neither of the above work.
In principle, your Cloud Functions can use any other Node.js module, the same way any standard Node.js server would. However, since Cloud Functions needs to build your module in the cloud, it needs to be able to locate those dependency modules from the cloud. This is where the issue lies.
Cloud Functions can load modules from any one of these places:
Any public npm repository.
Any web-visible URL.
Anywhere in the functions/ directory that firebase init generates for you, and which gets uploaded on firebase deploy.
In your case, from the perspective of functions/package.json, the ../server/ directory doesn't fall under any of those categories, and so Cloud Functions can't use your module. Unfortunately, firebase deploy doesn't follow symlinks, which is why that solution doesn't work.
I see two possible immediate fixes:
Move your server/ directory to be under functions/. I realize this isn't the prettiest directory layout, but it's the easiest fix while hacking. In functions/package.json you can then have a local dependency on ./server.
Expose your code behind a URL somewhere. For example, you could package up a .tar and put that on Google Drive, or on Firebase Cloud Storage. Alternatively, you can use a public git repository.
In the future, I'd love it if firebase deploy followed symlinks. I've filed a feature request for that in Firebase's internal bug tracker.
Firebase tools auto detects the available functions and deploy when we issue the below command
firebase deploy --only functions
But, we would like to use gcloud cli as it has more control for providing environment variables/for specifying a vpc connector.
Unfortunately, when using gcloud functions deploy, we need to specify the function name for each function.
Is it possible to get the list of http functions/triggers automatically from the source?
Update : As gcloud cli needs the type of the function (http/event), how to find out the type of the exported function automatically so that i can automate instead of specifying each function details?
If you are working with source code meant to be deployed to Cloud Functions by the Firebase CLI, you can write code to:
Load (require()) the module defined by index.js
Iterate its exports - each export is expected to be a function to deploy
There is nothing existing that will do this for you - you will have to write the code.
I've created a javascript script that will get information from this api. I'm trying to set this up inside of a google cloud function. I understand how you "require" or "import" a reference normally inside of a google cloud function, and even that you can make reference to a local file. When i downloaded the api.js from the source and try to reference it, i get "bomgarState is undefined" where bomgarState is the reference that i need. I understand that it is looking for a json when an api is being defined but i need to use the api.js. I know that this code can work,when you run this in collaboration with an html that sources said api, i have no problems. I have tried many different solutions such as "require(./api.js)", getting from url, and much more but am unsure if this is something that is supported at this point.
TLDR: can't make reference or source a javascript api inside of google cloud functions, had been sourcing using html, now that we moved to google cloud functions can't figure out how to make reference.
For you to import dependencies and libraries to your Cloud Functions, there are some available options that you can take with JavaScript, but only while using Node.js - without Node it's not possible.
For example, as the documentation Specifying dependencies in Node.js indicates:
A function is allowed to use external Node.js modules as well as local data. Dependencies in Node.js are managed with npm and expressed in a metadata file called package.json.
So, you can use with Node.js the npm to import your dependencies to your Cloud Functions.
Other options are indicated in the following posts from the Community - that I would recommend you to take a look at it - one, for example, would be using the 'gcloud' command to deploy your function. This way, it would upload and deploy with all files from your directory.
Google Cloud Functions include private library
How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?
I hope these articles help you, since they indicate the only available options, unfortunately, to upload dependencies on your Cloud Functions.
I am a developer with very little knowledge about Firebase Cloud Functions and programming in general. I followed the 'Get started' guide on the 'Firebase Cloud Functions' and have got to this point:
https://imgur.com/a/gjgEE
Now, where should I do all the Node.js coding work? Also, what is the index.js folder and where can I find it?
Running firebase init functions will have created the project files and index.js file + other files and directories as specified in the guide. You should run the init functions command in a directory that you make specifically for the project, not your home directory of \Users\Arjun Ram. I would recommend you do the following:
md FirebaseProject
cd FirebaseProject
firebase init functions
dir
This will create a folder for your project, cd into the folder, initialize the firebase project in the folder, and then list the contents of the folder to show you the files the command created. When you do dir, you should see a firebase.json and a functions/ directory which contains the index.js. You can then continue to proceed with the Getting Started guide.
I have is installed Parse Cloud Code on my Mac but I can't edit the main.js to edit my cloud code. What am I missing?
To Edit Cloud Code we have to first Traverse To the Cloud Code Folder
which in clude 3 Folders namely cloud,config,node_modules.
Your main.js will be in cloud folder.
open that and edit the main.js page
then save it.
After saving,
the main step is pushing the code to parse.com.
in parse console type command parse deploy
after successfull deploy your code will be on parse.com.
You need to use parse.com command line tool: you edit locally in your machine, and then deploy your code.
More info, in parse.com documentacion: https://parse.com/docs/js/guide#cloud-code