Search code examples
google-app-enginenext.jsgcloud

Cannot deploy Next.js app to GAE, couldn't found 'pages' folder build error


I'm working on this project that was firstly deployed to GAE last year; I have no problems at that time with the deploy using the gcloud CLI and an app.yaml file

Now, I needed to add a new page to the app and just wen for it, created the new page folder /newpage and an index.tsx file inside it, when I finished I run my project for testing and run a build for preparation to the deploy

When I ran the deploy command the CLI show me this error:

Build error occurred
Error: > Couldn't find a `pages` directory. Please create one under the project root
    at Object.findPagesDir (/workspace/node_modules/next/dist/lib/find-pages-dir.js:86:19)
    at /workspace/node_modules/next/dist/build/index.js:103:63
    at async Span.traceAsyncFn (/workspace/node_modules/next/dist/trace/trace.js:79:20)
    at async Object.build [as default] (/workspace/node_modules/next/dist/build/index.js:66:29)

This is my project structure from the beginning of the project

.next
build
components
models
node_modules
pages
public
services
styles
.gcloudignore
app.yaml
next.config.js
package.json
tsconfig.json

There was no changes to the folders structure after the last deployment

I found a suggestion for the build script but the error at the build during the deployment task is still the same

I just do this to the build script on package.json

"scripts": {
    "dev": "next dev",
    "build": "rm -rf ./build && NODE_ENV=production next build",
    "start": "next start -p 8080",
    "deploy": "npm run build && gcloud app deploy"

With that "rm" command remove the build folder and create a new one and force the NODE_ENV to production every time the build command is called but is still getting the same error

This is the next.config.js code

const nextConfig = {
    reactStrictMode: true,
    distDir: "build",
};

module.exports = nextConfig;

And this is the app.yaml code that I have created at the beginning of the project and is used for the deploy command

env: standard
runtime: nodejs14
instance_class: F1
service: panel
inbound_services:
   - warmup
automatic_scaling:
   min_idle_instances: 1
   min_instances: 1
handlers:
   - url: /.*
     secure: always
     script: auto
env_variables:
   NEXT_PUBLIC_MAPS_API_KEY: xxxx

Solution

  • ...Moving from comments to answer for easier discovery...

    April 11, 2023 release introduced a breaking change to Nodejs Apps

    For all the services using the Node.js runtime, npm run build now automatically runs during deployment if you have the npm build script defined in your package.json file.

    If you don't want it to run the build, then add "gcp-build":"" to the scripts part/section of your package.json file