next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Firebase example: couldn't find a `pages` directory

Open matchatype opened this issue 7 years ago • 58 comments

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]

matchatype avatar Sep 28 '19 14:09 matchatype

Hi, I'm not able to reproduce this with a minimal repro of the above structure. Can you provide more detailed steps to reproduce? Is the current working directory you're running Next.js from the project root?

Screen Shot 2019-09-28 at 17 51 55

ijjk avatar Sep 28 '19 15:09 ijjk

@ijjk the project structure is very similar to the with-firebase-hosting example. Yes, I'm running npm run serve from the project root.

matchatype avatar Sep 28 '19 16:09 matchatype

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.

matchatype avatar Sep 28 '19 16:09 matchatype

Can confirm it's an issue in 9.0.7 when using src/pages for production builds.

wouterds avatar Sep 28 '19 22:09 wouterds

same problem for me trying to deploy to firebase with nextjs 9.0.2 seems to work

yestoall avatar Sep 29 '19 18:09 yestoall

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.

merrywhether avatar Sep 30 '19 01:09 merrywhether

Working again in v9.0.8-canary.0, thanks for fixing so quickly guys!

wouterds avatar Oct 01 '19 18:10 wouterds

I am using 9.0.6 and can confirm this error as well. It was working before and then suddenly just broke itself. Cost me 2 days of debugging. Thank god someone finally reported this on Github... Tried updating to 9.0.8-canary.0 and still got the error. Rolling back to 9.0.4 for now.

cinnabarhorse avatar Oct 02 '19 05:10 cinnabarhorse

I am using 9.0.6 and can confirm this error as well. It was working before and then suddenly just broke itself. Cost me 2 days of debugging. Thank god someone finally reported this on Github... Tried updating to 9.0.8-canary.0 and still got the error. Rolling back to 9.0.4 for now.

How do you start your next app? Do you use a src folder?

Before 9.0.7 if you wanted to use a src folder you had to use a workaround and start your Next application like this next src. Since 9.0.7 they added official support and you can get rid of the last argument and start your app like this next.

wouterds avatar Oct 02 '19 06:10 wouterds

I'm using NextJS with firebase functions, so when I deploy I use next build src/client

cinnabarhorse avatar Oct 02 '19 06:10 cinnabarhorse

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.

notthatnathan avatar Oct 04 '19 19:10 notthatnathan

@notthatnathan I tried what seems to work for you but it's not working for me. Using cross-env NODE_ENV=production removed the error about pages not found, but I still have the error concerning the invalid production build. The error is Error: Could not find a valid build in the '/home/myhome/projectRootDirectory/next' directory! Try building your app with 'next build' before starting the server. When the next folder is in /home/myhome/projectRootDirectory/src/functions/next The next project and config is in /home/myhome/projectRootDirectory/src/app/next.config.js I tried many things like:

  • /home/myhome/projectRootDirectory/src/function/next
  • ../functions/next
  • ../functions/.next

Nothing works :-/

qlerebours avatar Oct 07 '19 07:10 qlerebours

not working for me again with 9.0.8, same page folder errors and /_app page size is huge at the same time

note: everything works with 9.0.2

yestoall avatar Oct 07 '19 08:10 yestoall

@qlerebours That's actually exactly what's happening for me, unfortunately.

notthatnathan avatar Oct 07 '19 11:10 notthatnathan

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)

Timer avatar Oct 07 '19 12:10 Timer

cc @jthegedus could you please fix the example to never allow a production build to boot in dev mode?

Timer avatar Oct 07 '19 12:10 Timer

@Timer I'd actually had that thought based on experimenting with NODE_ENV, but it's not fixing it for me. With dev: false in functions/index.js, deploy says it can't find a valid build in [project root]/.next and serve says it can't find a valid build in [project root]/dist/functions/.next.

The build seems fine and is in [project root]/dist/functions/next.

notthatnathan avatar Oct 07 '19 12:10 notthatnathan

@notthatnathan please provide a fully reproducing example so we can try to identify what's wrong with the setup.

Timer avatar Oct 07 '19 12:10 Timer

Well, I thought I'd reproduced it with the with-firebase-hosting example, but it actually works there with dev: false, so clearly I have something else not quite right. Sorry, nothing to see here.

notthatnathan avatar Oct 07 '19 13:10 notthatnathan

still not working with 9.1.0 seems like it trying to find next folder in different place...

in my case in my firebase.json i have... { "hosting": { "public": "dist/public", ... { "source": "**", "function": "app-next" } ], "predeploy": "yarn build-public" }, "functions": { "source": "dist/functions", ... } }

so it must try to find next folder inside dist/functions but its not working

any ideas?

yestoall avatar Oct 07 '19 19:10 yestoall