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

image

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

Most upvoted comments

I changed the package.json

from

"deploy": "firebase deploy",

to

"deploy": "NODE_ENV=production firebase deploy",

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 production mode.

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.js file should be edited, replacing:

var dev = process.env.NODE_ENV !== 'production'
var app = next({
  dev,
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
})

With:

var app = next({
  dev: false,
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
})

(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 distDir path 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, next was looking in dist/functions/app/next while the file was actually in dist/functions/next

The docs say distDir: '${path.relative(process.cwd(), __dirname)}/next' while most of the examples in the wild say distDir: 'next'. The former leads to the path problem listed above and the latter looks for the build file in a next folder inside the project root. The build file is in neither place.

If you hardcode the distDir to /dist/functions/next trying 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 the path module (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 after 9.0.6

In app.ts

import * as path from 'path'

...

const app = next({
  dev: false,
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../next` }
});

(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 directory errors, public directory errors, Could not find a valid build errors, and then the suggestions in a few issues of dev: false instead of the var dev = process.env.NODE_ENV !== 'production' version or node_env=production in your package.json being 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 Firebase examples)

src/
--app/
--functions/
--public/
--stylesheets

dist/
--functions/
----app/
----next/
--public/

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:

// src/functions/index.ts
next({ dev: false, conf: { distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` } })

What’s exactly your issue?

Quick note: I can run next as next src/app too, or npm run dev according 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:

  1. Downloaded the latest example of the with-firebase-hosting
  2. Changed the node engine to 10 "engines": { "node": "10" },
  3. Installed next@latest "next": "^9.3.4",
const app = next({
    dev,
    dir: path.join( __dirname, '../app'),
});

works fine for me

@xaphod Maybe you are missing to set your node environment to production like this:

NODE_ENV=production firebase emulators:start --inspect-functions --only functions,hosting

or by directly setting it in next configuration in app/index.js like this:

next({ dev: false })

Thanks @paintedbicycle after setting dev: false i 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.

{ distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` }

@yestoall

still doesn't work in 9.1.2
only with 9.0.6 i could deploy my app

any news?

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 dir option on the next() constructor), while also having deleted the source pages directory before building my final docker stage.

As a workaround, it appears that I can literally mkdir pages in 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). Screen Shot 2020-04-01 at 6 13 00 PM

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 on next version 9.3.0.

// app/index.js: 

const next = require('next');
const path = require('path');

const dev = process.env.NODE_ENV !== 'production';
console.log(`app/index: dev=${dev}`);
const app = next({ dev, conf: {distDir: `${path.relative(process.cwd(), __dirname)}/../functions/next` }});

module.exports = {
  app,
};
// functions/index.js:
const functions = require('firebase-functions');
const { app } = require('../app');

const handle = app.getRequestHandler();

exports.next = functions.https.onRequest(async (req, res) => {
  console.log('File: ' + req.originalUrl); // log the page.js file that is being requested
  await app.prepare();
  handle(req, res);
});

Error:

>  app/index: dev=true
⚠  Error: > Couldn't find a `pages` directory. Please create one under the project root
    at findPagesDir (/Users/tim/iPhone-dev/project/app/node_modules/next/dist/lib/find-pages-dir.js:3:170)

I made sure that next build has run, and I see its output under functions/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 for pages under functions/ which makes no sense. Is it looking for built stuff or the actual pages source in app/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 serve supporting hot-reloading, so the “local testing” I was suggesting doing with the firebase serve command was of a production build before pushing it to Firebase Hosting.

Using firebase serve is 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 the with-firebase-hosting example. 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=production to 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 distDir is [project root]/dist/functions/next.

And finally, npm run serve works if I change the dist dir to dist/functions/.next instead of dist/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.