EDIT:
I want to deploy a Nuxt3 app to Firebase Hosting. I've set the deployment preset for Nitro to "firebase" NITRO_PRESET=firebase, and the build step works fine. However, when I run firebase deploy, I get an error saying:
There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
I checked the .output/server directory, and saw that Nitro generated a file called index.mjs. I checked to see if Cloud Functions also works with .mjs files, but that doesn't seem to be the case. I looked for possible solutions but couldn't find anything; I tried reinstalling my modules, but no luck. Do I have something misconfigured or broken?
I'm following this tutorial:
https://nitro.unjs.io/deploy/providers/firebase
I'm using WSL2 Ubuntu on Windows 11, and the project folder is inside the linux filesystem.
Here is my package.json:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"#nuxtjs/google-fonts": "^3.0.0-0",
"#types/three": "^0.143.1",
"nuxt": "3.0.0-rc.8"
},
"dependencies": {
"firebase": "^9.9.3",
"sass": "^1.54.5",
"three": "^0.143.0",
"vuex": "^4.0.2",
"sass-loader": "^13.0.2"
}
}
My firebase.json:
{
"functions": {
"source": "./.output/server",
"runtime": "nodejs16",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
},
"hosting": {
"site": "my-site",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
}
After the build step:
Σ Total size: 19.6 MB (3.21 MB gzip)
✔ You can deploy this build using npx firebase deploy
After the deployment step:
i deploying functions, hosting
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
i functions: preparing codebase default for deployment
Error: There was an error reading .output/server/package.json:
.output/server/index.js does not exist, can't deploy Cloud Functions
It seems you are using the default node-server preset. To deploy the Nuxt 3 app to Firebase, you must set the NITRO_PRESET to firebase in your environment.
NITRO_PRESET=firebase
npm run build
The CLI should prompt you to run npx firebase deploy once the build is completed.
Then run the command and it will deploy the application to Firebase hosting and functions.
You can read more about this in the documentation.
Related
I was following the in-app-purchase part in Google Codelab, But after I finish all steps and finally run firebase deploy this error has appeared.
I have read many solutions about the simillar problems, for example, remove node-modules and package-lock file then re-install them, down grade node version to 14, downgrade firebase-admin, firebase-functions...
But none of them works for me.
I'm totally desperate now. Please somebody help me. I already googled almost 72hours and I found nothing.
And here is the contents of terminal show up after I run firebase deploy.
% firebase deploy
=== Deploying to 'server-259035'...
i deploying firestore, functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint /Users/functions
> eslint --ext .js,.ts .
Running command: npm --prefix "$RESOURCE_DIR" run build
> functions# build /Users/functions
> tsc
✔ functions: Finished running predeploy script.
i firestore: reading indexes from firestore.indexes.json...
i cloud.firestore: checking firestore.rules for compilation errors...
✔ cloud.firestore: rules file firestore.rules compiled successfully
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
i functions: preparing codebase default for deployment
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/firestore' is not defined by "exports" in /Users/functions/node_modules/firebase-admin/package.json
And package.json file.
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"watch": "tsc --watch",
"serve": "firebase emulators:start",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "lib/index.js",
"dependencies": {
"camelcase-keys": "^8.0.0",
"firebase-admin": "^11.0.1",
"firebase-functions": "^3.22.0",
"google-auth-library": "^8.0.2",
"googleapis": "^105.0.0",
"jest": "^28.1.3",
"lodash": "^4.17.21",
"node-apple-receipt-verify": "^1.12.1"
},
"devDependencies": {
"#types/node-apple-receipt-verify": "^1.7.1",
"#typescript-eslint/eslint-plugin": "^5.27.0",
"#typescript-eslint/parser": "^5.27.0",
"eslint": "^8.16.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.26.0",
"firebase-functions-test": "^2.3.0",
"typescript": "^4.7.2"
},
"private": true
}
I had the same issue when updated my package.json packages. I had to make the following change in order to fix it:
Before:
import firestore from "firebase-admin/lib/firestore";
After:
import firebaseAdmin from "firebase-admin";
** Add the prefix firebaseAdmin. to all the firestore references, eg: firebaseAdmin.firestore.Timestamp;*
I changed the following files:
app-store.purchase-handler.ts
google-play.purchase-handler.ts
iap.repository.ts
It happens with in_app_purchases flutter firebase example due to updated package names and their content. Full error is: Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/firestore' is not defined by "exports" in functions/node_modules/firebase-admin/package.json
You need to replace: import firestore from "firebase-admin/lib/firestore"; with import firebaseAdmin from "firebase-admin";
Now you need to go your .ts files and replace the same import above with the new one and correct the references for example: firestore.Timestamp now will be firebaseAdmin.firestore.Timestamp
All the files you need to make changes are these ones:
app-store.purchase-handler.ts
google-play.purchase-handler.ts
iap.repository.ts
I’ve fixed, upgraded and updated codelab example backend functions for verifying in app purcashe on backend. You can find updated code here, it works as expected for me with node 18.
https://github.com/vbalagovic/iap-firebase-backend
I'm able to deploy my Next.js project to Firebase Functions successfully as per this post. However, when I try to visit my project in the browser, I get a Error: could not handle the request error, and in the Functions log I see this error message:
Error: Could not find a production build in the '/workspace/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
at Server.readBuildId (/workspace/node_modules/next/dist/next-server/server/next-server.js:151:355)
at new Server (/workspace/node_modules/next/dist/next-server/server/next-server.js:3:120)
at NextServer.createServer (/workspace/node_modules/next/dist/server/next.js:1:2935)
at process._tickCallback (internal/process/next_tick.js:68:7)
I'm not sure how to resolve this though... Adding below my config files:
package.json
{
"main": "server.js", // my next.js app init file
"scripts": {
"dev": "FIREBASE_AUTH_EMULATOR_HOST=localhost:9099 next dev",
"start": "next start",
"build": "next build",
"build:functions": "tsc",
"lint": "npm run lint:next && npm run lint:functions",
"lint:functions": "eslint --ext .js",
"lint:next": "next lint",
"deploy": "firebase deploy --only database,hosting,functions:nextServer",
"emulate": "firebase emulators:start --import=./emulator-data --export-on-exit",
"test": "jest --runInBand --detectOpenHandles",
"cypress": "cypress open"
},
"dependencies": {
// including next
}
}
firebase.json
{
"functions": {
"source": ".",
"runtime": "nodejs10",
"ignore": [
"**/.next/cache/**",
"**/__tests__/**",
"**/components/**",
"**/cypress/**",
"**/emulator-data/**",
"**/functions/**",
"**/layouts/**",
"**/node_modules/**",
"**/pages/**",
"**/public/**",
"**/utils/**",
"**/.*",
"**/*.log",
"**/cypress.json",
"**/database.rules.json",
"**/firebase.json"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build:functions"
]
},
"hosting": {
"target": "my_app",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [{
"source": "**",
"function": "nextServer"
}]
}
}
I assume that, once deployed, Firebase Functions environment would run npm run build and npm run start scripts. Or is the workflow different? Perhaps my scripts are incorrect?
Similar to scripts in package.json files, the predeploy and postdeploy lines are executed before and after a call to firebase deploy when using the Firebase CLI (documented here).
When the Firebase CLI uploads your code to the Cloud Functions staging servers, it is expected to have already been compiled and only needs its runtime dependencies installed. Once an upload has completed, the staging server will execute npm install --production and then prepare your function to handle requests (essentially performing a cold-start, without actually executing the function). If either step fails, you will be shown an error like you have. If both steps succeed, your code with the dependencies installed is persisted as an image and saved to Cloud Storage, ready to use when requests to be handled come in.
In your case, the error seems to be that your .next folder is missing the compiled version of your application.
There are two possible causes of this, either your build prior to deployment is incomplete or when your files are uploaded, something in your ignore list that is required by the deployed code is missing.
For the first part, you modified the predeploy hook in my previous answer from:
{
"functions": {
"source": ".",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
...
}
incorrectly to:
{
"functions": {
"source": ".",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build:functions"
]
},
...
}
By changing this, you aren't building your next app prior to deploying the code to the staging server.
In regards to the second part, the deployed next application may be missing some of its dependencies - you can either fine tune the ignore list or you can perform the next build step on the server using a postinstall script in your package.json file:
{
"main": "server.js", // my next.js app init file
"scripts": {
...
"build:next": "next build",
...
"postinstall": "npm run build:next"
},
"dependencies": {
// including next
}
}
Note: If your main file is just JavaScript, you no longer need the typescript compile step defined in npm run build:functions and it can be removed.
I was this error, my solution was:
First, in the packege.json file, in the section scripts, we must modify the property “build”: "next build && next export", and a new property "export": "next export"
Second, we must establish the component and data folders in the root of the project
Third: use the command:
a)You must make log in, so type: firebase login
b)select de option "Configure files for Firebase Hosting and (optionally) set
up GitHub Action deploys"
c)in the question: if you have a build process
for your assets, use your build's output directory?
What do you want to use as your public directory?
You must type: out (really important, it's a folder output)
d) You must type: npm run build
f) last step: type: firebase deploy
package.json
out folder
I am trying to deploy my Firebase Cloud Function with:
firebase deploy --only functions:helloWorld
My problem is that the command is sometimes successful, but most times it is failing with the error above. What might be the cause, and how to fix it?
I came across a similar question and tried many of the recommended solutions (clearing the npm cache and reinstalling the dependencies). Both solutions do not work reliably for me.
Here is the full console output:
=== Deploying to '<my-firebase-project>'...
i deploying functions
✔ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (84.76 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: current functions in project: myFunction1(us-central1), myFunction2(us-central1), myFunction3(us-central1)...
i functions: uploading functions in project: helloWorld(us-central1)
i functions: updating Node.js 10 function helloWorld(us-central1)...
⚠ functions[helloWorld(us-central1)]: Deployment error.
Build failed: npm ERR! Maximum call stack size exceeded
npm ERR! A complete log of this run can be found in:
npm ERR! /builder/home/.npm/_logs/2020-10-15T17_38_33_530Z-debug.log; Error ID: 49341d49
Functions deploy had errors with the following functions:
helloWorld
To try redeploying those functions, run:
firebase deploy --only "functions:helloWorld"
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
Part of the problem is that I don't know where to find the full logs that the firebase deploy command output says are in /builder/home/.npm/_logs/2020-10-15T17_38_33_530Z-debug.log
And here are my firebase/functions config files:
./functions/index.js:
const functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
./firebase.json:
{
"functions": {
"predeploy": [
],
"source": "functions",
"runtime": "nodejs10"
},
"emulators": {
"functions": {
"port": 5001
},...
}
...
}
./functions/package.json:
{
"name": "functions",
"description": "Backend - Firebase Cloud Functions",
"version": "1.0.0",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14.3.0"
},
"dependencies": {
"config": "^3.3.1",
"core-util-is": "^1.0.2",
"crypto-js": "^4.0.0",
"firebase-admin": "^9.1.1",
"firebase-functions": "^3.11.0",
"myapp_config": "file:./config"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
Dev stack:
macOS 10.15+
node version = v14.3.0
Turns out the issue was how I was building the local dependency myapp_config in the line
"myapp_config": "file:./config". I re-architected my app to remove [the need for] this line altogether and so far have not seen the deployment error again.
The ./config indicated on that line was actually a symlink to the config folder on my project root that I created with: ln -s ./config ./functions/config originally intended to deliver project-wide configuration-related modules into my functions scripts without having to manually copy over those files into functions. I suppose that much as Firebase Functions supports local Node.js dependencies, the CLI/npm somehow gets confused by or doesn't work well with symlinks (my guess is that it sees ./config and its symlink ./functions/config and goes into some infinite loop between them)
This other question sheds more light on the code structure I had and the problem I was trying to solve in the first place.
I am not able to deploy my app into production. I only have firebase hosting and functions. On firebase serve everything works well.
I obtain the following error:
i deploying functions, hosting
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
+ functions: required API cloudbuild.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (46.72 KB) for uploading
+ functions: functions folder uploaded successfully
i hosting[library-app-d49fe]: beginning deploy...
i hosting[library-app-d49fe]: found 19 files in public
+ hosting[library-app-d49fe]: file upload complete
i functions: updating Node.js 10 function app(us-central1)...
! functions[app(us-central1)]: Deployment error.
Function failed on loading 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
Functions deploy had errors with the following functions:
app
To try redeploying those functions, run:
firebase deploy --only "functions:app"
To continue deploying other features (such as database), run:
firebase deploy --except functions
My firebase.json looks like this
{
"hosting": {
"public": "public",
"rewrites": [{
"source": "**",
"function": "app"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
and my app.js in functions folder has the following export:
exports.app = functions.https.onRequest(app);
I am sorry if I don't provide the necessary details but I am clueless of what the issue might be.
I have deployed my NextJS project (adapted from https://github.com/zeit/next.js/tree/master/examples/with-firebase-hosting) to firebase, but when i browse to the URL i get "Internal Server Error".
Deployment did run through successfully:
i deploying database, functions, hosting
+ database: rules ready to deploy.
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
+ runtimeconfig: all necessary APIs are enabled
+ functions: all necessary APIs are enabled
i functions: preparing src/functions directory for uploading...
i functions: packaged src/functions (571.83 KB) for uploading
+ functions: src/functions folder uploaded successfully
i hosting: preparing src/public directory for upload...
! Warning: Public directory does not contain index.html
i Progress: [....................................................................................................]
+ hosting: 3 files uploaded successfully
i starting release process (may take several minutes)...
i functions: updating function next...
+ functions[next]: Successful update operation.
+ functions: all functions deployed successfully!
+ Deploy complete!
So i feel perhaps it is something to do with the structure of my project? Or firebase.json config? Could someone have a look and let me know?
Here is my firebase.json which is in the root of my project:
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"source": "src/functions"
},
"hosting": {
"public": "src/public",
"rewrites": [
{
"source": "**/**",
"function": "next"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}