next.js: Firebase example: couldn't find a `pages` directory
Bug report
My project has a custom structure but so far it worked pretty well. Since I updated to v9.0.7 I get this error:
Error: > Couldn't find a pagesdirectory. Please create one under the project root at Object.findPagesDir [...]
So far running next src/app worked just fine. Is there anything different I should do now?
Screenshot

System information
- OS: [macOS]
- Version of Next.js: [9.0.7]
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 35
- Comments: 58 (13 by maintainers)
Commits related to this issue
- fix firebase deploy error https://github.com/zeit/next.js/issues/8893 — committed to hirose504/next-todo by hirose504 7 years ago
- added fix for #8893 (#10370) — committed to vercel/next.js by deleted user 7 years ago
- added fix for #8893 (#10370) — committed to chibicode/next.js by deleted user 7 years ago
- never run next in firebase function in dev mode - related to #8893 — committed to maerzhase/next.js by maerzhase 6 years ago
I changed the package.json
from
to
and it worked!
This error occurs because the Firebase server is trying to boot Next.js in development mode.
As such, this error is expected – Next.js is telling you that there’s no
pages/directory to start the dev server with.You need to boot the Next.js production server using strictly
productionmode.I’m closing this because Next.js is correctly erroring, as it should. I could only reproduce this locally when trying to incorrectly boot the production server.
Likely, the
src/functions/index.jsfile should be edited, replacing:With:
(it’s never valid to run a production build in dev mode, this error is preventing you from a bad experience)
I too have not been able to deploy since
9.0.6. I’ve tested each release as they came out, but nothing worked. I put some time into it recently and below is my solution.The
distDirpath in both the docs and the above snippets didn’t work for me. I’m wondering if this helps anyone else or perhaps it was something unique about the way I have my project set up?Looking at the error logs while using the version from the examples and the above snippets,
nextwas looking indist/functions/app/nextwhile the file was actually indist/functions/nextThe docs say
distDir: '${path.relative(process.cwd(), __dirname)}/next'while most of the examples in the wild saydistDir: 'next'. The former leads to the path problem listed above and the latter looks for the build file in anextfolder inside the project root. The build file is in neither place.If you hardcode the
distDirto/dist/functions/nexttrying to fix the path issue shown above, that will work locally and it’ll start to deploy and get much farther than before but then all your cloud functions will explode so you need to use the relative version using thepathmodule (see code below).I was using the
distDir: 'next'for a very long time because it was in the original @jthegedus blog posts I believe (and so I think a lot of people with older projects likely have the same), but then everything broke after9.0.6In
app.ts(Note the
/../in the distDir path)Things deploy fine for me now.
If that works for others, we really need to get all the docs and all the old blog posts updated. It was really hard tracking this down since there were lots of path changes lately and I was seeing
pages directoryerrors,public directoryerrors,Could not find a valid builderrors, and then the suggestions in a few issues ofdev: falseinstead of thevar dev = process.env.NODE_ENV !== 'production'version ornode_env=productionin yourpackage.jsonbeing the culprit (neither of which seem to be). I found it to be a game of wac-a-mole where once I fixed one error, I’d get the other.LMK if that solves this issue for you and I’ll submit a PR with some updates to the firebase examples. Don’t want to make any changes if this was only a quirk of my install.
My setup (should be pretty normal - based on the
Firebaseexamples)same problem for me trying to deploy to firebase with nextjs 9.0.2 seems to work
Is this getting fixed someday? I’m having the same issue, seems it’s been a while.
@scottie-schneider I got it working with what @SuigetsuSake1 commented:
What’s exactly your issue?
Quick note: I can run next as
next src/apptoo, ornpm run devaccording to my scripts in the screenshot on default port 3000, it is the built project that will crash:npm run serve, which runs fine till v9.0.6.[edit] Sorry, it will take you to setup a firebase project, but the example repo will reproduce the issue.
@ryudice what worked for me:
"engines": { "node": "10" },"next": "^9.3.4",works fine for me
@xaphod Maybe you are missing to set your node environment to
productionlike this:or by directly setting it in next configuration in
app/index.jslike this:Thanks @paintedbicycle after setting
dev: falsei had the same issue with the distDir that was not found and after using your fix it is working, however i had the next directory inside “functions” , i am using the with-firebase-hosting-and-typescript sample though so it may not be up to date.@yestoall
The issue is open, has labels “good first issue” and “help wanted” so you’re free to open a PR to solve the issue or try and investigate.
I’m seeing this too, arising from a docker config that starts the app from a few directories above the app directory (for which I’ve previously set the
diroption on thenext()constructor), while also having deleted the sourcepagesdirectory before building my final docker stage.As a workaround, it appears that I can literally
mkdir pagesin my app directory and everything works without having to make any other changes. Seems like it’s a really shallow check and then things proceed with the actual built files as before.Do you people actually have acceptable performance using a Firebase Function (in reallity just a Google Cloud Function behind the scenes)?
I ended up switching to a e2-micro instance on Google Cloud (just a vm) with the next.js app running 24/7. For my use case this is acceptable money wise, and I don’t need the theoretical infinite scalability of a cloud function to be honest.
After all the config fighting and testing, I just feel it’s not there yet. In theory it seemed like a good idea and a nice one as well (firebase deploy and there you go). This is my own experience with my current projects, so take this with a grain of salt. 🤷♂
Additionally, it seems that cloud functions meant to be publicly accessed (like the all important functions/index.js) need to be specifically assigned permissions moving forward.
Once I had this working, without setting the access level for allUsers, I got an ‘your app is not authorized to view this resource’ error when going to my hosting URL. Hope that helps someone coming next (or me, when I forget I solved this and come back to this thread in 2 months).
Reference: https://forum.serverless.com/t/setting-iam-policies-for-google-http-functions-since-all-new-gcloud-http-functions-after-nov-1-2019-will-default-private/8686
I’m also stuck when trying to run firebase emulator with
firebase emulators:start --inspect-functions --only functions,hosting. I’m onnextversion 9.3.0.Error:
I made sure that
next buildhas run, and I see its output underfunctions/next: folders like cache, server, static… I don’t know where the “project root” is (I suppose functions/next), and I don’t see a pages directory anywhere after build.Update: did some console.log in
find-pages-dir.js findPagesDir(), looks like it’s looking forpagesunderfunctions/which makes no sense. Is it looking for built stuff or the actualpagessource inapp/pages? I suppose the latter, which is the issue.A bad answer I know, but I won’t have time to look at this (and submit a correction) for another day or two. My most up to date usage of Next.js with Firebase is here.
Off the top of my head, I don’t recall
firebase servesupporting hot-reloading, so the “local testing” I was suggesting doing with thefirebase servecommand was of a production build before pushing it to Firebase Hosting.Using
firebase serveis to check that Firebase url rewriting works as opposed to using it to do the development of the app.Apologies for not getting back sooner.
@Timer any news with that issue?
@notthatnathan please provide a fully reproducing example so we can try to identify what’s wrong with the setup.
cc @jthegedus could you please fix the example to never allow a production build to boot in dev mode?
Same here, with latest canary (
9.0.8-canary.4). I’m closely following thewith-firebase-hostingexample. I was able to deploy before I updated from v7 to 9.FWIW, I’m able to skip the ‘pages’ directory error by adding
cross-env NODE_ENV=productionto the beginning of the deploy script: https://github.com/zeit/next.js/blob/canary/examples/with-firebase-hosting/package.json#L13.But then I run into a “Could not find a valid build in the ‘[project root]/.next’ directory!” error, which is weird since my
distDiris[project root]/dist/functions/next.And finally,
npm run serveworks if I change the dist dir todist/functions/.nextinstead ofdist/functions/next, otherwise I get a similar error: “Could not find a valid build in the ‘[project root]/dist/functions/.next’ directory!” It seems across the board my dist dir name is being ignored in favor of the default.next.