将Next.js应用程序部署到App Engine标准[Nodejs]并收到500错误 [英] Deployed a Next.js application to App Engine Standard [Nodejs] and got a 500 error
问题描述
我将其部署到具有nodejs8运行时的应用程序引擎中,并获得了500.我正在部署next.js应用程序,并在检查StackDriver后得到了.似乎.next可能会被忽略.错误如下:
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. The error is as follows:
throw new Error("Could not find a valid build in the '".concat(this.distDir, "' directory! Try building your app with 'next build' before starting the server."));
Error: Could not find a valid build in the '/srv/build' directory! Try building your app with 'next build' before starting the server. at Server.readBuildId (/srv/node_modules/next/dist/server/next-server.js:753:15) at new Server (/srv/node_modules/next/dist/server/next-server.js:80:25) at module.exports (/srv/node_modules/next/dist/server/next.js:6:10) at Object.<anonymous> (/srv/server.js:10:13) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Function.Module.runMain (module.js:694:10)
我的package.json文件如下:
My package.json file looks like:
{
"name": "emails",
"private": true,
"version": "1.0.0",
"main": "server.js",
"scripts": {
"dev": "NODE_ENV=development node server.js",
"build": "next build",
"lint": "standard",
"prestart": "next build",
"start": "NODE_ENV=production node server.js",
"appspot-deploy": "gcloud app deploy --project=email-app-219521",
"deploy": "gcloud app deploy"
},
"standard": {
"parser": "babel-eslint"
},
"license": "ISC",
"dependencies": {
"@firebase/app-types": "^0.3.2",
"@material-ui/core": "^3.2.0",
"@material-ui/icons": "^3.0.1",
"@zeit/next-sass": "^1.0.1",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"express-rate-limit": "^3.2.1",
"express-session": "^1.15.6",
"firebase-admin": "^6.0.0",
"isomorphic-unfetch": "^3.0.0",
"memorystore": "^1.6.0",
"next": "^7.0.1",
"node-sass": "^4.9.3",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"styled-jsx-plugin-sass": "^0.3.0"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"eslint": "^5.6.1",
"webpack": "^4.20.2"
},
"engines": {
"node": "8.x.x"
}
}
我的app.yaml文件如下:
And my app.yaml file looks like:
runtime: nodejs8
env_variables:
NODE_ENV: production
handlers:
- url: /.*
script: server.js
我将在8080端口上通过Express服务我的项目.
And Im serving up my project with express on port 8080.
推荐答案
当应用程序返回500个错误时,请确保在 https://console.cloud.google.com/logs/viewer .仔细检查您是否正在查看"GAE应用程序"资源选择器.
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.
查看错误消息,看来您的应用程序中不存在.next
文件夹.该.next
文件夹通常是通过构建步骤"生成的文件夹,我确实发现您在package.json
中将"build": "next build"
作为script
.
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
.
您不应该使用prestart
来执行此构建步骤,首先是因为App Engine在实例启动时未运行prestart
,而且还因为总体上这会降低性能.
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:
-
在部署之前在计算机上生成
.next
,为此,您可以将deploy
脚本更改为:"deploy": "npm run build && gcloud app deploy"
. (还要确保.gcloudignore
文件不包含.next
或不包含.gitignore
文件的内容)
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)
在部署后在Google Cloud的服务器上运行此构建步骤:Node.js App Engine运行时将执行任何gcp-build
脚本(如果存在).这意味着您可以将此脚本:"gcp-build": "npm run build"
添加到您的package.json
.这样做时,建议您将.next
添加到.gcloudignore
文件中,以使.next
文件夹在部署时不会上载.
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.
此外,请注意,您可以将app.yaml
简化为runtime: nodejs8
:
In addition, note that you can simplify your app.yaml
to just runtime: nodejs8
:
-
NODE_ENV
自动设置为production
,您可以将其删除 - 您的处理程序部分等同于默认部分.
NODE_ENV
is automatically set toproduction
, you can remove it- Your handlers section is equivalent to the default one.
这篇关于将Next.js应用程序部署到App Engine标准[Nodejs]并收到500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!