Reinitializing Firebase Functions project - firebase

I already have a Firebase functions that are currently working fine. These functions were deployed when I was using Windows machine. Today I thought to modify something in the functions with my Mac machine. When I am trying to reinitialize the same project, the index.js comes with the default code without my functions that I deployed.
Following are the steps I am performing in order to reinitialize the project.
firebae init
Select: Functions: Configure and deploy Cloud Functions
Use an existing project
I am selecting my project name
language: JavaScript
Do you want to use ESLint to catch probable bugs and enforce style? Yes
Do you want to install dependencies with npm now? (Y/n) Yes
Now, when I go and check the index.js in the function folder it has only one default code.
const functions = require('firebase-functions');
However, I can see the functions with my custom code at https://console.cloud.google.com/functions.
please help. Thanks for your time and help

Initializing a project with the Firebase CLI doesn't not rebuild any source code that you might have previously deployed. You should be storing that in some sort of source control, such as git, in order to share it between people, projects, and machines. If you don't use source control, you will have to manually copy the files between machines.

You can just re-run firebase init functions to regenerate the firebase config (not download your source code)
I had to reinitialize my firebase functions (title of the question) because I was getting some errors in firebase-debug.log about firebase.json. I deleted it and wanted to regenerate it. I didn't have the issue the question poster had (I guess he literally ran firebase init and expected the code to be transferred from his windows computer to his mac, which is why Doug said "you will have to manually copy the files between machines".)
I was getting the error, Error: Not in a Firebase app directory (could not locate firebase.json). I ran firebase init functions in the root directory of the project which already had a functions directory, and the error was fixed.
The following showed up:
=== Functions Setup
A functions directory will be created in your project with sample code
pre-configured. Functions can be deployed with firebase deploy.
? What language would you like to use to write Cloud Functions? TypeScript
? Do you want to use ESLint to catch probable bugs and enforce style? Yes
? File functions/package.json already exists. Overwrite? No
i Skipping write of functions/package.json
? File functions/.eslintrc.js already exists. Overwrite? No
i Skipping write of functions/.eslintrc.js
? File functions/tsconfig.json already exists. Overwrite? No
i Skipping write of functions/tsconfig.json
? File functions/tsconfig.dev.json already exists. Overwrite? No
i Skipping write of functions/tsconfig.dev.json
? File functions/src/index.ts already exists. Overwrite? No
i Skipping write of functions/src/index.ts
? File functions/.gitignore already exists. Overwrite? No
i Skipping write of functions/.gitignore
? Do you want to install dependencies with npm now? Yes

Related

Firebase Deploy: Error: Could not detect language for functions at

I'm trying to deploy my firebase project, but Im getting the following error:
=== Deploying to 'my-proj'...
deploying firestore, functions, hosting
cloud.firestore: checking firestore.rules for compilation errors...
[W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]).
cloud.firestore: rules file firestore.rules compiled successfully
Error: Could not detect language for functions at
any thoughts?
In my case, I missed functions init:
firebase init functions
documentation
In my case I use firebase deploy --only hosting command
I can't tell what else is perhaps going wrong with your code, but I also had an error that looked like this part:
[W] undefined:undefined - Ruleset uses old version (version [1]). Please update to the latest version (version [2]).
Some of your firebase rules are written using version 1. You may need to go in and add
rules_version = '2';
to the top of your firebase rule set, specifically for Firebase Firestore. You can do this in the firebase console, or in the firestore.rules in your project - which may be a newly generated file in your project.
For anyone else who ends up here. I had setup firebase static hosting 1yr+ ago, and at that point in time hosting was a "function". Since then, hosting is now a distinct offering separate of functions.
You need to convert the previous "default" function (which 'was' hosting) to hosting offering.
Delete (or edit) firebase.json in the root directory of your firebase project.
Firebase init hosting -> This will create a new "public" folder.
Copy your previous functions/public folder to overwrite the new one.
Delete the previous functions folder altogether.
Now hosting is fixed and you can deploy again.
Firebase init functions -> This will create a new functions folder, and all of the boilerplate json and .js files.
The problem I was having is the functions folder had both hosting and functions stepping on each other within the functions directory. The "today" process puts hosting in a separate folder altogether.
Please update your rule to version 2 as the error message says.

Angular Universal + Firebase cloud functions deployment issue

I've been stuck on this issue for some time now.
My Angular project version is 9.0.3.
The #angular/fire version is 6.1.4
I have followed the guide here
The project builds and renders for the local Express server I have.
The problem occurs when I deploy the project to Firebase with a cloud function that will render the application.
Running the command ng deploy gives the below error
Error: ENOENT: no such file or directory, open '/home/teebo/Development/motf/dist/motif/browser/index.html'
This issue is linked to a reported issue here
I have investigated and found that in the project's dist folder, a index.original.html file is generated instead of index.html.
I have opened an issue here with no feedback from the maintainers.
If there is anything I am missing or there's something I can do to circumvent this issue, please share and any suggestions are appreciated.
Any suggestion, comment, anything.
I have found a way to circumvent this issue.
This is not a proper solution for the ng deploy issue but at least I can deploy the ssr function.
Because my dist folder is nested in another dist folder (Which does not include the browser build folder), that is why I was getting the error when running ng deploy. Below are steps I've followed to at least be able to deploy.
Run ng deploy (It will error out with the above error but will build the app and create a dist folder).
Copy the browser folder to the dist folder that does not have it (In my case I have copied it to a dist folder one level up).
Deploy the ssr function by running firebase deploy --only functions:ssr (You can change functions:ssr to whatever name you have named your function).
This worked for me;
It would save time to write an npm script to do all this but then; I am not sure if my firebase/universal config is wrong or there is a bug with #angular/fire.

Firebase Deploy causing multiple errors and not deploying

Hello I am following this tutorial in order to do push notifications to multiple devices at once in Expo React Native using Firebase's cloud functions. The push tokens are stored in my RealTime-Database, but when I attempt to integrate this step after running firebase login and firebase init, I updated my index.js file to contain this code. After saving this file, I attempt to do firebase deploy and I get 70 errors. I tried to deploy it from my app directory, and didnt work, and I tried to do it from my functions directory and that didn't work. I deleted my node_modules and package-lock, and I tried to rerun expo again and clearing my npm cache. Nothing worked. I already had my firebase app initialized somewhere else, could that be the reason why? But my App itself still works, so I am not getting an error saying that the Firebase app is being initialized twice. I really dont know what else do to. Is the ESLint causing all of these errors? Should I run firebase init again without adding ESLint? Please let me know. My errors are below.
When deploying your code, by default, your code is linted using a tool called ESLint.
The "errors" you see are eslint complaining about trivial things like missing semicolons at the end of a line, using ' instead of ", improperly indented code, among other things. If you ever don't understand what a rule is trying to do, you can visit https://eslint.org/docs/rules/{rule-name} (such as https://eslint.org/docs/rules/semi).
As shown in the error message, you can use eslint . --fix to automatically fix most of these. Just enter into your functions directory and execute eslint . --fix.
Most IDEs have a keyboard shortcut for automatically formatting your code to your rules. In VSCode for example, this shortcut is Alt+Shift+F.

Why does firebase warn "* You are currently outside your home directory" during an init?

I am trying to deploy my first firebase app. I am getting the message "* You are currently outside your home directory" I googled it and found this reply
"commented on Dec 6, 2016
Just to make sure you're aware. If someone is experiencing the same problem with with the command firebase init
Make the files .firebaserc and firebase.json manually and the deploy should work normally."
I do not know where to create them or what the should contain.
I have also gone to https://www.npmjs.com/package/firebase-tools to try to fix this problem.
If any one can help with this problem I would appreciate it.
If anyone else worried about this just keep going. I continued with the deployment and it deployed OK.
download firebase CLI binary (in case you didn't download it yet. this is an .exe file if you use windows).
copy the downloaded file into your project's root folder (Folder which contains all the files and folders of your projects).
run the firebase-tools-instant-win_2.exe (firebase CLI binary).
a command window will open.
execute all your command in there.
Initialize a Firebase project
Many common tasks performed using the CLI, such as deploying to a Firebase project, require a project directory. You establish a project directory using the firebase init command. A project directory is usually the same directory as your source control root, and after running firebase init, the directory contains a firebase.json configuration file.
To initialize a new Firebase project, run the following command from within your app's directory:
I used firebase init project in my project's directory to get it to work

Can not see the Firebase function deployed

I followed the following steps:
The Firebase CLI (Command Line Interface) requires Node.js and npm, which you can install by following the instructions on https://nodejs.org/
Installing Node.js also installs npm
Once you have Node.js and npm installed, install the Firebase CLI via npm:
npm install -g firebase-tools
This installs the globally available firebase command. To update to the latest version, re-run the same command
Initialize your project:
a. Run firebase login to log in via the browser and authenticate the firebase tool.
b.Go to your Firebase project directory or create the directory
c. Run firebase init functions
The tool gives you an option to install dependencies with npm. It is safe to decline if you want to manage dependencies in another way.
Select associated firebase project
Select Y to install dependencies with npm
Move to directory setup firebase functions
Edit index.js file with the function you created
Run the firebase use --add to add your Firebase project
Run firebase deploy --only functions to deploy the function
After all this I get the message in the terminal at deploy was completed but in the Firebase console, when i click on Functions tab there are no functions listed!?
Quick Tip: Make sure you are exporting the function you are trying to deploy in your index.js file. Your firebase project will deploy but Cloud Functions won't be available unless they are exported.
Make sure you save the file after uncommenting the default function and then use
firebase deploy
For Cloud Functions, it is required to add your function to the special exports object (it is a Node's way of making the function accessible outside of the current file)
Make sure to have index.js in your functions directory:
Example of a function:
// Import the Firebase SDK for Google Cloud Functions.
const functions = require('firebase-functions');
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();
// Your function declaration. Example of a fired function when a user is created in Auth feature.
exports.myFunction = functions.auth.user().onCreate(async (user) => {
// ... your code here.
});
Then for deployment follow these steps:
First, if not done, make sure to have firebase-tools installed:
$ npm install -g firebase-tools
And initialised: $ firebase init
For full deployment:
$ firebase deploy
OR for functions deployment
$ firebase deploy --only functions
OR to deploy specific functions
$ firebase deploy --only functions:function1,functions:function2
A good read with a very useful example: https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#7
I went through the same issue recently, while performing Actions on Google Node.js Client Library Version 1 Migration Guide. to Node.js Client Library V2 (That I highly recommend) It took me a while to figure out what what was happening. At the I couldn't really figure out what the problem was! So here is what I did and it worked for me:
Make sure you have a backup copy of your cloud functions (index.js) and maybe your package.json (Just in case you don't want to remember what packages you previously had installed - Could be annoying sometimes).
Delete the entire functions directory from your project folder.
Re-launch firebase CLI with firebase init and choose Functions
Once your cloud function have been initialized, CD into the functions folder and Redeploy it using firebase deploy --only functions.
If everything goes well 😃, you should now see your function deployed on your firebase dashboard console.
N.B: Google recently released the Node.js Client Library version 2 (v2) in April 16th 2018 with a lot of new features. After April 16th, 2018, new features on Actions on Google will no longer be added to v1 of the client library. If you want to use new features, you must migrate to v2 client library.
In addition, the v1 client library does not support Dialogflow v2. If you need Dialogflow v2 functionality, you’ll also need to migrate to v2 of the client library.
Hope this helps 👍.
In step 7, you have to uncomment the sample function in there and save the file. Then, in the output of the deploy command, you will be given a url for the created helloWorld function.
I had exactly the same problem and I solved it by making sure the index.js file containing all my functions was saved on the "functions" folder inside the project folder. I am using vs code so I just clicked on file/save as and selected the correct folder.
#Learn2Code
I had the exact same issue.
Ensure that in your index.js file, you export your function before initializing your app.
Now, go ahead and run firebase deploy from your project directory.
For example:
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/messages').push({original: original});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref.toString());
});
const admin = require('firebase-admin');
admin.initializeApp();
Make sure you're running at least version 3.5.0 of firebase-tools. To check which version you have, run:
firebase --version
If you're running the default setup, you can update firebase-tools using:
npm install -g firebase-tools
Had the same situation. The problem was that when I was doing
$ firebase deploy --only "myFunction"
That filter name: myFunction, was not exactly the same as the name of the function I was trying to deploy. Silly mistake but took me a day to realize...
To clarify one issue - it appears as though your index.js file inside the functions folder must export functions created within the same file (similar to what Fran had said).
It seems trying to organize your files into subfolders will not work properly with Firebase functions - same rules apply for using firebase serve to test locally (must create codeinside functions/index.js).
Hope this helps someone!
1) Make sure you are exporting the function you are trying to deploy in your index.js file.
2) Write 'use-strict' at the top of your file (index.js) then use console to deploy your function
Check your "default project" at firebase init. Select one with similar name was my mistake. ;)
Use firebase projects:list and firebase use <project> to make sure the Firebase CLI's "current project" is set correctly regardless of what folder you're in.
Example:
> firebase projects:list
✔ Preparing the list of your Firebase projects
┌──────────────────────┬─────────────────────┬──────────────────────┐
│ Project Display Name │ Project ID │ Resource Location ID │
├──────────────────────┼─────────────────────┼──────────────────────┤
│ alpha │ alpha (current) │ [Not specified] │
├──────────────────────┼─────────────────────┼──────────────────────┤
│ beta │ beta │ [Not specified] │
└──────────────────────┴─────────────────────┴──────────────────────┘
2 project(s) total.
> firebase use beta
Now using project beta
I had this error as well. I had copied a working function running on Google Cloud Functions from a previous project and could not figure out why it would not show up once deployed.
I needed to wrap my function in functions.https.onRequest(), which is not required on normal cloud functions.
Had the same issue.
In my case solved by using firebase deploy wihtout any --only, which revealed better error messaging and I had to setup billing in Cloud Console for the project. The setup routine can be triggered by selecting Could Functions in the Console.
I had another issue in which the Service Account was missing from the Project, so I setup a fresh project through the Console first and then added this to Firebase.
Make sure you init firebase on step back from your firebase functions. and also firebase functions name must be functions.
It works for me
One dumb gotcha I just ran into, I was running "firebase deploy" from the top level folder (one above the /functions sub-folder), and it deployed "successfully" but I never saw it in Firebase itself. It wasn't until I cd'd into /functions and re-ran firebase deploy that the full deployment actually worked.

Resources