Firebase cannot understand what targets to deploy - firebase

When deploying the following hello-world equivalent code I get the error shown in the end:-
$ ls -lR
.:
total 8
-rw-r--r-- 1 hgarg hgarg 3 Aug 29 14:55 firebase.json
drwxr-xr-x 2 hgarg hgarg 4096 Aug 29 11:56 functions
./functions:
total 4
-rw-r--r-- 1 hgarg hgarg 1678 Aug 29 11:56 index.js
firebase.json looks like this:-
{}
and index.json like this:-
'use strict';
const functions = require('firebase-functions');
exports.search = functions.https.onRequest((req, res) => {
if (req.method === 'PUT') {
res.status(403).send('Forbidden!');
}
var category = 'Category';
console.log('Sending category', category);
res.status(200).send(category);
});
But deploying fails:-
$ firebase deploy
Error: Cannot understand what targets to deploy. Check that you specified valid targets if you used the --only or --except flag. Otherwise, check your firebase.json to ensure that your project is initialized for the desired features.
$ firebase deploy --only functions
Error: Cannot understand what targets to deploy. Check that you specified valid targets if you used the --only or --except flag. Otherwise, check your firebase.json to ensure that your project is initialized for the desired features.

it would be better to pre-populate the firebase with the default options. I choose that I wanted to use only hosting the firebase.json should have be created with the default hosting option.
{
"hosting": {
"public": "public"
}
}
or you try run firebase init again.

Faced similar issue. In firebase.json file (in hosting parameter), we have to give the name of directory that we want to deploy (in my case, I was already in the directory, which I wanted to deploy, hence I put "." in hosting specification). It solved the error for me.
{
"hosting": {
"public": ".",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}

I was facing this issue when running:
firebase emulators:exec --only firestore 'npm tst'
The problem was that on firebase.json must have a property for the emulator you want. So in my case I added a "firestore": {} on firebase.json and worked.

I faced this problem too because at the beginning of my Firebase project setup, I only initialized the hosting feature. Later on, when I wanted to deploy firestore security rules with the Firebase CLI, my initialization process was not complete for this command to work as expected.
I could not run firebase deploy --only firestore:rules
because my firebase.json file was not initialized with defaults.
Easiest and most adequate way to fix this problem is to run the firebase init command again to setup all features you want to use. You could do it manually but you could miss details that the command line interface can setup for you in the exact way it needs to be for defaults.
Solution:
Run the firebase init command again
...and make sure to initialize every feature you are currently using. Take care not to overwrite important configs if you already have some by carefully reading the Firebase CLI instructions that are asked by the init command.

Firebase reads package.json to read details of the functions target. This file was missing from my project directory, as I had moved files around after doing an init.
Creating a clean directory and doing a firebase init functions inside it created all the required files and folders to get started.

I think you are missing on one of the following things -
a) you should run firebase init outside of the main project where index.html is.
b) select hosting option after running firebase init by pressing SPACE
c) Please give folder name which contain index.html in it.
And your project will be up running.

Related

Firebase functions: RangeError [ERR_FS_FILE_TOO_LARGE]: File ... is greater than 2 GB

Been deploying a ton w/ no problems. Made a small change and now it bonks on deploy.
I rolled back a few commits and still get same error. Tried deploying only 1 function, same issue. Deleted a bunch of log files, etc. Same.
Quotas all look fine.
There's about 20 functions in the project.
i functions: Loaded environment variables from .env.
i functions: preparing functions directory for uploading...
[2023-01-17T07:51:09.242Z] RangeError [ERR_FS_FILE_TOO_LARGE]: File size (5067767843) is greater than 2 GB
at new NodeError (node:internal/errors:372:5)
at readFileHandle (node:internal/fs/promises:377:11)
at async getSourceHash (/Users/adrian/.nvm/versions/node/v16.16.0/lib/node_modules/firebase-tools/lib/deploy/functions/cache/hash.js:12:18)
at async packageSource (/Users/adrian/.nvm/versions/node/v16.16.0/lib/node_modules/firebase-tools/lib/deploy/functions/prepareFunctionsUpload.js:61:30)
at async prepare (/Users/adrian/.nvm/versions/node/v16.16.0/lib/node_modules/firebase-tools/lib/deploy/functions/prepare.js:131:36)
at async chain (/Users/adrian/.nvm/versions/node/v16.16.0/lib/node_modules/firebase-tools/lib/deploy/index.js:35:9)
at async deploy (/Users/adrian/.nvm/versions/node/v16.16.0/lib/node_modules/firebase-tools/lib/deploy/index.js:79:5)
Error: Could not read source directory. Remove links and shortcuts and try again.
What file is it referring to?
Appreciate any ideas.
A new type of log file had been generated which didn't match the current ignore pattern in firebase.json. So the log file was being processed during deploy.
Solution: update ignore pattern to cover any .log files
firebase.json
...
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": ["node_modules", ".git", "*.log"]
}
]
...

How do I deploy Cloud Functions while ignoring existing functions?

Say I have the following four functions in my Firebase projects:
openDoor(europe-west1)
closeDoor(europe-west1)
openWindow(europe-west1)
closeWindow(europe-west1)
Now, these functions live in two separate Node packages, i.e. one that contains openDoor and closeDoor and another one that contains openWindow and closeWindow.
Error
If I try to run firebase deploy from the package with the door functions, the following error will be thrown (in non-interactive mode):
Error: The following functions are found in your project but do not exist in your local source code:
openWindow(europe-west1)
closeWindow(europe-west1)
This is a problem because it will cancel any CD workflow that tries to deploy these functions.
Force delete
There is an option to force-delete any existing functions:
-f, --force delete Cloud Functions missing from the current
working directory without confirmation
However, I want the opposite. I want to keep all existing functions.
Theoretical workaround
There is one workaround that I found would work in theory, which is:
yes N | firebase deploy --interactive
Piping N into the interactive deploy command, which will answer N to the deletion prompt:
The following functions are found in your project but do not exist in your local source code:
openWindow(europe-west1)
closeWindow(europe-west1)
If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)
The problem now is that I am using https://github.com/w9jds/firebase-action to deploy the functions, which means that I need to have a built-in Firebase solution.
You can make use of the new codebases feature in Firebase.
By specifying a codebase in your firebase.json functions configuration, this problem is solved. The Firebase CLI will no longer prompt you to delete other functions as it only considers the functions of the same codebase.
If your firebase.json previously looked like this:
{
"functions": {
"source": "cloud_functions",
"ignore": [...],
"predeploy": [...],
"postdeploy": [...]
}
}
You only need to add "codebase": "<name>" to the config:
{
"functions": {
"source": "cloud_functions",
"codebase": "window",
"ignore": [...],
"predeploy": [...],
"postdeploy": [...]
}
}
The deploy will now look like this:
i functions: updating Node.js 16 function window:openWindow(europe-west1)...
i functions: updating Node.js 16 function window:closeWindow(europe-west1)...
Note that the actual function name does not change, i.e. the function will still only be called openWindow (without the prefix) in the Firebase / Google Cloud Console. So this is basically the perfect solution to the problem.
Alternatively, you can also specify the function names when performing deployment.
firebase deploy --only functions:openDoor,functions:closeDoor

Dynamic firebase.json file

It is quite difficult to keep track of the CSP rules in the firebase.json file, therefore I decided to add a script that constructs the proper rules. Now I need to manually update the firebase.json file every time I am changing something in the CSP configuration. Is there any way to dynamically configure firebase? I was hoping to achieve it with a simple renaming as it worked for many services grunt.json -> grunt.js I guess I can use some sort of templating but I wonder if there's a built-in mode to dynamically construct headers, rules etc for firebase.
There isn't a built-in way to generate firebase.json on command. I would recommend creating a script, e.g. npm run deploy that first generates your firebase.json content and then runs firebase deploy. That way you can make sure it's always regenerated before deployment.
// configureFirebase.js
const fs = require('fs');
const config = {
hosting: {
// ...
}
}
fs.writeFileSync(__dirname + '/firebase.json', JSON.stringify(config));
// package.json
{
"scripts": {
"deploy": "node configureFirebase.js && firebase deploy"
}
}

How to deploy express app on firebase hosting

I have built an express app and the folder structure is below.
Then i created firebase init hosting on a dummy folder and copied the firebase.json and .firebase files
I created index.js file
const functions = require('firebase-functions')
const app = require('./app');
exports.widgets = functions.https.onRequest(app);
firebase.json
{
"hosting": {
"public": "public",
"rewrite":[{
"source": "**",
"function": "widgets"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Also copied the index.html generated by firebase to public folder
On deployment i am getting index.html
If i delete index.html and run as localhost i am getting below output
How can i get the express app executed (as shown in localhost) instead of index.html on firebase deploy.
Edit 1
I am following link.
When i run firebase serve, i am getting this error
AssertionError [ERR_ASSERTION]: missing path at Module.require (module.js:583:3) at require (internal/module.js:11:18) at InitializeFirebaseAdminStubs (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:231:18) at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:451:9 at Generator.next () at C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:7:71 at new Promise () at __awaiter (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:3:12) at main (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:421:12) at Object. (C:\Users\alaksandarjesus\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:511:5)
When i tried to run firebase deploy
E:\ogst-server-firebase\functions>firebase deploy
=== Deploying to 'ogst-server-95fcc'...
i deploying functions, hosting
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
Error: An unexpected error has occurred.
The reason for this is missing public folder, and i created manually (which was expected to be created with firebase init functions).
E:\ogst-server-firebase\functions>firebase deploy
=== Deploying to 'ogst-server-95fcc'...
i deploying functions, hosting
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i hosting[ogst-server-95fcc]: beginning deploy...
i hosting[ogst-server-95fcc]: found 0 files in public
+ hosting[ogst-server-95fcc]: file upload complete
i hosting[ogst-server-95fcc]: finalizing version...
+ hosting[ogst-server-95fcc]: version finalized
i hosting[ogst-server-95fcc]: releasing new version...
+ hosting[ogst-server-95fcc]: release complete
+ Deploy complete!
Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/ogst-server-95fcc/overview
Hosting URL: https://ogst-server-95fcc.firebaseapp.com
Now my deployment is successful. But i am getting 404
Answer
In the index.js file (following the above link), i did not change
module.exports = functions.https.onRequest(app); //wrong
to
exports.app = functions.https.onRequest(app); //correct
Thanks to all for the support
Firebase Hosting prefers to serve static content over rewrites that get sent to Cloud Functions. In other words, if a request could be served by any static content, that content will always take precedence over a rewrite to Cloud Functions.
If you want your web site root page to be served by Cloud Functions, this means you should not have an index.html in your public folder, since Firebase Hosting is finding that first.

Firebase Serve Error

I am new to firebase and am trying to make a simple app that utilizes user authentication. At this point in the project I am trying to run firebase on a local server using CLI commands.
I have set up firebase init and firebase deploy. When I type firebase serve on my project folder i get the response,
"an unexpected error has occurred".
Below i am attaching the contents of my firebase-debug.log file. Any help would be appreciated. Thanks
command requires scopes:
["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase",...
[debug] [2017-06-11T17:09:09.607Z] > authorizing via signed-in user
TypeError: Cannot read property 'public' of undefined
Look in your firebase.json file, which you should have in the directory where you're running firebase serve. It should look something like this:
{
"hosting": {
"public": "app",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
If it doesn't have that "hosting" key, then you'll get that Cannot read property 'public' of undefined error because firebase serve tries to access .hosting.public.
I faced this issue today, I ran it with --debug and found out that I've installed npm/node as sudo user, running following:
firebase serve
was giving me this error:
Error: An unexpected error has occurred.
When I ran it with sudo, I was able to deploy hosting and functions locally:
sudo firebase serve --debug --only hosting,functions
1) create a folder called "public" and put your files inside.
2) edit the firebase.json and just write this:
{
"hosting": {
"public": "public"
}
}
This appears to be a bug - that ideally should be resolved with Firebase Init. I have logged a support ticket with Firebase, and would encourage others to do so as well.
I think you may have skipped an initialization step by accident (I did the same thing on my first run-through)
Try this (from your same project directory):
1) firebase init
2) When prompted for which services you'll need make sure that Database AND Hosting options have their markers highlighted green (tab to Hosting with the spacebar). It's easy to select just database and let it run its configuration and assume it configures all of the bulleted options below it if you do not watch closely because the stdout info isn't very clear. Now hit Enter
3) Accept the default database settings as you probably did before, and when asked "What do you want to use as your public directory? (public)", hit Enter.
At this point you should be good to go spin up a local firebase web server...
4) try 'firebase serve', hit Enter, and you should get a verification that hosting files are being served from /public
Hope this helps. Good luck.
As for me the error is (--debug attr)
Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/firebase-tools/node_modules/#google-cloud/functions-emulator/logs'
So the solution will be to run with sudo

Resources