将Next.js应用程序部署到App Engine Standard [Nodejs]并得到500错误

dav*_*vis 5 google-app-engine google-appengine-node

我使用nodejs8运行时部署到app引擎并获得了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)
Run Code Online (Sandbox Code Playgroud)

我的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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的app.yaml文件看起来像:

runtime: nodejs8
env_variables:
  NODE_ENV: production
handlers:
- url: /.*
  script: server.js
Run Code Online (Sandbox Code Playgroud)

而我正在使用端口8080上的快递服务我的项目.

Ste*_*ren 10

当您的应用程序返回500个错误时,请务必通过https://console.cloud.google.com/logs/viewer查看Stackdriver日志记录中的应用程序stdoutstderr日志.仔细检查您是否正在查看"GAE应用程序"资源选择器.

查看错误消息,.next您的应用程序中似乎不存在该文件夹.此.next文件夹,通常是通过"构建步骤"生成一个文件夹,我看的确是你有"build": "next build"一个script在你的package.json.

您不应该使用prestart执行此构建步骤,首先是因为App Engine不在prestart实例启动时运行,而且因为通常这对性能不利.

您有两种方法来创建此文件夹:

  1. .next在部署之前在您的机器上生成,为此,您可以将deploy脚本更改为:"deploy": "npm run build && gcloud app deploy".(并确保.gcloudignore文件不包含.next或不包含.gitignore文件的内容)

  2. 部署后在Google Cloud的服务器上运行此构建步骤:Node.js App Engine运行时将执行任何gcp-build脚本(如果存在).这意味着您可以将此脚本添加"gcp-build": "npm run build"到您的package.json.这样做时,我建议您添加.next.gcloudignore文件中,以便.next在部署时不上载文件夹.

此外,请注意,您可以简化app.yaml到只是runtime: nodejs8:

  • NODE_ENV自动设置为production,您可以将其删除
  • 您的处理程序部分等同于默认部分.