I deployed to app engine with nodejs8 runtime and got a 500. Im deploying a next.js application, and upon reviewing StackDriver I get. It appears .next might be getting ignored.
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 your deploy
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 your package.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 to production
, you can remove itThis happened to me when I removed a file that one of my pages (in the /pages folder) happened to be referencing.
Either restore that file, or remove the file that is referencing the missing file.
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