将 Next.js 应用程序部署到 App Engine Standard [Nodejs] 并出现 500 错误
Deployed a Next.js application to App Engine Standard [Nodejs] and got a 500 error
我使用 nodejs8 运行时部署到 App Engine 并获得了 500。我部署了一个 next.js 应用程序,在查看 StackDriver 时我得到了。看来 .next 可能会被忽略。错误如下:
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 文件如下所示:
{
"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 文件如下所示:
runtime: nodejs8
env_variables:
NODE_ENV: production
handlers:
- url: /.*
script: server.js
我正在使用端口 8080 上的 express 服务我的项目。
当您的应用程序返回 500 个错误时,请确保在位于 https://console.cloud.google.com/logs/viewer 的 Stackdriver Logging 中查看应用程序的 stdout
和 stderr
日志。仔细检查您是否正在查看 "GAE Application" 资源选择器。
查看错误消息,您的应用程序中似乎不存在 .next
文件夹。这个 .next
文件夹通常是通过 "build step" 生成的文件夹,我确实看到你的 package.json
中有 "build": "next build"
作为 script
。
您不应该使用 prestart
来执行此构建步骤,首先是因为 App Engine 在实例启动时不会 运行 prestart
,还因为通常这会很糟糕为了表演。
您有两种创建此文件夹的方法:
在部署之前在您的计算机上生成 .next
,为此,您可以将 deploy
脚本更改为:"deploy": "npm run build && gcloud app deploy"
。 (还要确保 .gcloudignore
文件不包含 .next
或不包含 .gitignore
文件的内容)
运行 部署后 Google 云服务器上的此构建步骤:Node.js App Engine 运行time 将执行任何 gcp-build
脚本(如果存在)。这意味着您可以将此脚本:"gcp-build": "npm run build"
添加到您的 package.json
。这样做时,我建议您将 .next
添加到 .gcloudignore
文件,这样 .next
文件夹就不会在部署时上传。
此外,请注意,您可以将 app.yaml
简化为 runtime: nodejs8
:
NODE_ENV
自动设置为production
,可以去掉
- 您的处理程序部分与默认部分相同。
我刚刚设法让它发挥作用。这是我的发现:
首先,它不适用于 Yarn。当有 yarn.lock
个文件时,gcp-build
命令似乎没有 运行。
现在 package.json
,使用以下脚本:
"scripts": {
"gcp-build": "next build",
"start": "next start -p $PORT"
}
并确保将 Next 和 next.config.js
中所需的所有模块声明为 dependencies(不是 devDependencies)
仅供参考,我的 app.yaml
中只有 runtime: nodejs10
,我只有脚本在 pages
文件夹和 next.config.js
中
当我删除我的一个页面(在 /pages 文件夹中)碰巧引用的文件时,我遇到了这种情况。
恢复该文件,或删除引用丢失文件的文件。
我使用 nodejs8 运行时部署到 App Engine 并获得了 500。我部署了一个 next.js 应用程序,在查看 StackDriver 时我得到了。看来 .next 可能会被忽略。错误如下:
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 文件如下所示:
{
"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 文件如下所示:
runtime: nodejs8
env_variables:
NODE_ENV: production
handlers:
- url: /.*
script: server.js
我正在使用端口 8080 上的 express 服务我的项目。
当您的应用程序返回 500 个错误时,请确保在位于 https://console.cloud.google.com/logs/viewer 的 Stackdriver Logging 中查看应用程序的 stdout
和 stderr
日志。仔细检查您是否正在查看 "GAE Application" 资源选择器。
查看错误消息,您的应用程序中似乎不存在 .next
文件夹。这个 .next
文件夹通常是通过 "build step" 生成的文件夹,我确实看到你的 package.json
中有 "build": "next build"
作为 script
。
您不应该使用 prestart
来执行此构建步骤,首先是因为 App Engine 在实例启动时不会 运行 prestart
,还因为通常这会很糟糕为了表演。
您有两种创建此文件夹的方法:
在部署之前在您的计算机上生成
.next
,为此,您可以将deploy
脚本更改为:"deploy": "npm run build && gcloud app deploy"
。 (还要确保.gcloudignore
文件不包含.next
或不包含.gitignore
文件的内容)运行 部署后 Google 云服务器上的此构建步骤:Node.js App Engine 运行time 将执行任何
gcp-build
脚本(如果存在)。这意味着您可以将此脚本:"gcp-build": "npm run build"
添加到您的package.json
。这样做时,我建议您将.next
添加到.gcloudignore
文件,这样.next
文件夹就不会在部署时上传。
此外,请注意,您可以将 app.yaml
简化为 runtime: nodejs8
:
NODE_ENV
自动设置为production
,可以去掉- 您的处理程序部分与默认部分相同。
我刚刚设法让它发挥作用。这是我的发现:
首先,它不适用于 Yarn。当有 yarn.lock
个文件时,gcp-build
命令似乎没有 运行。
现在 package.json
,使用以下脚本:
"scripts": {
"gcp-build": "next build",
"start": "next start -p $PORT"
}
并确保将 Next 和 next.config.js
中所需的所有模块声明为 dependencies(不是 devDependencies)
仅供参考,我的 app.yaml
中只有 runtime: nodejs10
,我只有脚本在 pages
文件夹和 next.config.js
当我删除我的一个页面(在 /pages 文件夹中)碰巧引用的文件时,我遇到了这种情况。
恢复该文件,或删除引用丢失文件的文件。