Deployed a Next.js application to App Engine Standard [Nodejs] and got a 500 error
When your application is returning 500 errors, make sure to look at the stdout
and stderr
logs of your application in Stackdriver Logging at https://console.cloud.google.com/logs/viewer. Double check that you are looking at "GAE Application" resource picker.
Looking at the error message, it seems that the .next
folder does not exist in your app. This .next
folder is a folder that is usually generated via a "build step", and I see indeed that you have "build": "next build"
as a script
in your package.json
.
You should not be using prestart
to perform this build step, first because App Engine does not run prestart
on instance startup, but also because in general this would be bad for performances.
You have two ways to create this folder:
Generate
.next
on your machine before deploying, to do so, you can change yourdeploy
script to be:"deploy": "npm run build && gcloud app deploy"
. (And also make sure that the.gcloudignore
file does not contain.next
or does not include the content of the.gitignore
file)Run this build step on Google Cloud's servers after deploying: The Node.js App Engine runtime will execute any
gcp-build
script if present. This means that you can add this script:"gcp-build": "npm run build"
to yourpackage.json
. When doing so, I recommend you to add.next
to the.gcloudignore
file so that the.next
folder does not get uploaded at deployment time.
In addition, note that you can simplify your app.yaml
to just runtime: nodejs8
:
NODE_ENV
is automatically set toproduction
, you can remove it- Your handlers section is equivalent to the default one.
I've just managed to make it work. Here's what I found:
First of all, it doesn't work with Yarn. It seems gcp-build
command does not run when there is yarn.lock
file.
Now in package.json
, use following scripts:
"scripts": {
"gcp-build": "next build",
"start": "next start -p $PORT"
}
And make sure to declare Next and all modules needed in next.config.js
as dependencies (not devDependencies)
FYI I only have runtime: nodejs10
in my app.yaml
and only scripts I have are in pages
folder and next.config.js