【问题标题】:GCP App Engine and Cloud Build: Cannot find module '/workspace/server.js'GCP App Engine 和 Cloud Build:找不到模块“/workspace/server.js”
【发布时间】:2021-11-29 02:18:41
【问题描述】:

TLDR:抱歉,这是历史上最长的问题,但我希望这对任何有类似问题的用户来说是全面的。我的应用程序从云外壳成功部署到我的域;但是,当我尝试云构建时,我得到了 Cannot find module '/workspace/server.js' 该错误可能与我在 app.yaml 中的构建处理程序有关,或者与我的 cloudbuild.yaml 有关。

解决方案:在 app.yaml 标准中使用正确的处理程序并正确设置您的 cloudbuild.yaml

我在同时使用 App Engine 和 Cloud Build 时遇到问题。我正在使用 Cloud Build 通过我的 Github 存储库设置 CICD。我认为这个问题是由于我没有将生产构建部署到应用程序引擎。我能够通过以下方式成功手动部署(开发版):

gcloud app deploy

现在,我的 Cloud Build 遇到了问题。特别是,我正在尝试运行 flex 环境,但我一直在“package.json”的“scripts”部分中找不到“start”,也没有找到“server.js”文件。”但是我的 package.json 有启动脚本?

我也尝试了标准环境而不是 flex,但我无法弄清楚处理程序。我尝试了几十个例子。我已经包含了标准环境 app.yaml,所以你可以看到它。

这是我的 package.json:

{
  "name": "flier-mapper",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@firebase/storage": "^0.8.4",
    "@parcel/transformer-sass": "^2.0.0",
    "@react-google-maps/api": "^2.7.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "bootstrap": "^5.1.3",
    "cors": "^2.8.5",
    "emailjs-com": "^3.2.0",
    "firebase": "^9.2.0",
    "firebase-admin": "^10.0.0",
    "firebase-functions": "^3.16.0",
    "react": "^17.0.2",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-ga": "^3.3.0",
    "react-helmet": "^6.1.0",
    "react-icons": "^4.3.1",
    "react-pricing-table": "^0.3.0",
    "react-router-dom": "^5.3.0",
    "react-scripts": "4.0.3",
    "react-tabs": "^3.2.3",
    "stripe": "^8.188.0",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

这是我的 cloudbuild.yaml。我也试过删除 dir 行:

steps:

- name: "gcr.io/cloud-builders/npm"
  dir: 'frontend'
  args: ['install']
  timeout: "5m"

- name: "gcr.io/cloud-builders/npm"
  dir: 'frontend'
  args: ["run", "build"]
  timeout: "5m"

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: 
    - "-c"
    - |
        cp app.yaml ./build
        cd build
        gcloud app deploy
  timeout: "1600s"

timeout: 60m

这是我的 flex app.yaml:

runtime: nodejs
env: flex

# This sample incurs costs to run on the App Engine flexible environment.
  # The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

这是我在标准环境中尝试过的众多处理程序之一:

runtime: nodejs14

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 1

handlers:
  # Serve all static files with url ending with a file extension
  - url: /(.*\..+)$
    static_files: build/\1
    upload: build/(.*\..+)$
  # Catch all handler to index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html

  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

编辑

所以,我让它在标准环境中运行,这对 https 请求很有用。这是我开始工作的 app.yaml:

runtime: nodejs14

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 2

handlers:

- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

但是,现在我的问题是:

Error: Cannot find module '/workspace/server.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {

这与上面的问题基本相同。

编辑 2

我想知道是不是因为现在我正在运行生产版本,所以我需要添加正确的处理程序。但是如上所述,我在网上尝试了几十种各种答案的组合,但无济于事。

这是我尝试过的另一个标准 app.yaml:

runtime: nodejs16

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 2

handlers:
  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

  - url: /
    static_files: build/index.html
    upload: build/index.html

  - url: /
    static_dir: build

这与我的一个包裹有关吗?如果有,是哪一个?我知道没有什么特别的......

我也将它添加到我的 package.json 中,但它也没有做任何事情:

"start": "PORT=8080 react-scripts start",

编辑 3 我试过这些帖子:Error: Cannot find module '/workspace/server.js' could not find module workspace/server.js Error: Cannot find module '/workspace/server.js' upon node js deploy on google app engine https://medium.com/@calebmackdaven/so-you-want-to-start-using-google-cloud-ce9054e84fa8

此 app.yaml 基于云构建;但是,我现在收到一个新错误“在此服务器上找不到请求的 URL /。”:

runtime: nodejs16

# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
  instances: 2

handlers:
  - url: /static/js/(.*)
    static_files: build/static/js/\1
    upload: build/static/js/(.*)
  - url: /static/css/(.*)
    static_files: build/static/css/\1
    upload: build/static/css/(.*)
  - url: /static/media/(.*)
    static_files: build/static/media/\1
    upload: build/static/media/(.*)
  - url: /(.*\.(json|ico))$
    static_files: build/\1
    upload: build/.*\.(json|ico)$
  - url: /
    static_files: build/index.html
    upload: build/index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html
  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

我的日志说:

Static file referenced by handler not found: build/index.html

编辑 4 我尝试了不同的 cloudbuild.yaml:

steps:

- name: node
  entrypoint: npm
  args: ['install']

- name: node
  entrypoint: npm
  args: ['run', 'build']

- name: 'bash'
  args: ['gcloud app deploy']

但是,现在我在 npm run build 阶段遇到的错误是:

Error message "error:0308010C:digital envelope routines::unsupported"

我检查了this answer,所以我尝试了不同的节点版本 14,但仍然遇到同样的问题。我尝试了以下方法,但得到了同样的错误:

"start": "react-scripts --openssl-legacy-provider start"

这是我的错误:

Step #1: Error: error:0308010C:digital envelope routines::unsupported
Step #1:     at new Hash (node:internal/crypto/hash:67:19)
Step #1:     at Object.createHash (node:crypto:130:10)
Step #1:     at module.exports (/workspace/node_modules/webpack/lib/util/createHash.js:135:53)
Step #1:     at NormalModule._initBuildHash (/workspace/node_modules/webpack/lib/NormalModule.js:417:16)
Step #1:     at /workspace/node_modules/webpack/lib/NormalModule.js:452:10
Step #1:     at /workspace/node_modules/webpack/lib/NormalModule.js:323:13
Step #1:     at /workspace/node_modules/loader-runner/lib/LoaderRunner.js:367:11
Step #1:     at /workspace/node_modules/loader-runner/lib/LoaderRunner.js:233:18
Step #1:     at context.callback (/workspace/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
Step #1:     at /workspace/node_modules/babel-loader/lib/index.js:59:103 {
Step #1:   opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
Step #1:   library: 'digital envelope routines',
Step #1:   reason: 'unsupported',
Step #1:   code: 'ERR_OSSL_EVP_UNSUPPORTED'
Step #1: }
Step #1: 
Step #1: Node.js v17.1.0
Finished Step #1
ERROR
ERROR: build step 1 "node" failed: step exited with non-zero status: 1

当我在 app.yaml 中指定 v16 时,为什么它尝试使用 Node.js v17.1.0?

编辑 5

我尝试指定 Node v16.13.0:

steps:
- name: node: "16.13.0"
  entrypoint: npm
  args: ['install']

- name: node: "16.13.0"
  entrypoint: npm
  args: ['run', 'build']

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: [gcloud app deploy]

但是,它根本没有构建:

Your build failed to run: failed unmarshalling build config cloudbuild.yaml: yaml: line 2: mapping values are not allowed in this context

所以我尝试添加一个替换:

--- 
steps: 
  - 
    args: 
      - install
    entrypoint: npm
    name: "node= $_NODE_VERSION"
  - 
    args: 
      - run
      - build
    entrypoint: npm
    name: "node= $_NODE_VERSION"
  - 
    args: 
      - "gcloud app deploy"
    entrypoint: bash
    name: gcr.io/cloud-builders/gcloud
substitutions: 
  $_NODE_VERSION: v16.13.0

但我得到了:

Your build failed to run: generic::invalid_argument: invalid build: invalid build step name "node= ": could not parse reference: node=

如果我尝试节点:“16.13.0”,错误类似:

Your build failed to run: failed unmarshalling build config cloudbuild.yaml: yaml: line 2: mapping values are not allowed in this context

好的,这个错误只是由于间距。我尝试如下更新我的 cloudbuild.yaml:

steps:
- name: node:16.13.0
  entrypoint: npm
  args: ['install']

- name: node:16.13.0
  entrypoint: npm
  args: ['run', 'build']

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: [gcloud app deploy]

现在我可以构建了。但是,现在我遇到了 gcloud 命令的问题:

Step #2: bash: gcloud app deploy: No such file or directory

编辑 6

我在兜圈子。我的新错误是原来的错误:

Error: Not Found
The requested URL / was not found on this server.

但至少我可以构建云构建。所以它必须是我的 app.yaml 或 cloudbuild.yaml。

steps:
- name: node:16.13.0
  entrypoint: npm
  args: ['install']

- name: node:16.13.0
  entrypoint: npm
  args: ['run', 'build']

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: 
    - "-c"
    - |
        cp app.yaml ./build
        cd build
        gcloud app deploy
Step #2: bash: gcloud app deploy: No such file or directory

解决方案 我终于让它工作了!我正在更改要构建的目录,但不应该如此。所以这是我的工作 cloudbuild.yaml 和 app.yaml 文件:

cloudbuild.yaml

steps:
- name: node:16.13.0
  entrypoint: npm
  args: ['install']

- name: node:16.13.0
  entrypoint: npm
  args: ['run', 'build']

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: 
    - "-c"
    - |
        cp app.yaml ./build
        gcloud app deploy

app.yaml

--- 
handlers: 
  - 
    secure: always
    static_dir: build/static
    url: /static
  - 
    secure: always
    static_files: build/\1
    upload: build/.*\.(json|ico|js)$
    url: /(.*\.(json|ico|js))$
  - 
    secure: always
    static_files: build/index.html
    upload: build/index.html
    url: .*
manual_scaling: 
  instances: 1
runtime: nodejs16

【问题讨论】:

  • 您可以参考类似的 Stackoverflow 案例,其中的错误与您得到的相同。 Google App Engine ErrorDeploying app to GAE如果有帮助请告诉我!
  • 感谢您的回复!关于第一篇文章,用户使用不支持处理程序的 flex 引擎,我无法让 flex 运行以在云构建上创建反应应用程序。关于第二篇文章,我什至没有使用任何服务器,只是前端创建反应应用程序,所以我的 package.json 在我部署的目录中,它有一个启动脚本。

标签: reactjs google-app-engine google-cloud-platform google-cloud-build app.yaml


【解决方案1】:

解决方案 我终于让它工作了!我正在更改要构建的目录,但不应该如此。所以这是我的工作 cloudbuild.yaml 和 app.yaml 文件:

cloudbuild.yaml

steps:
- name: node:16.13.0
  entrypoint: npm
  args: ['install']

- name: node:16.13.0
  entrypoint: npm
  args: ['run', 'build']

- name: "gcr.io/cloud-builders/gcloud"
  entrypoint: bash
  args: 
    - "-c"
    - |
        cp app.yaml ./build #not sure if this is necessary
        gcloud app deploy

app.yaml

--- 
handlers: 
  - 
    secure: always
    static_dir: build/static
    url: /static
  - 
    secure: always
    static_files: build/\1
    upload: build/.*\.(json|ico|js)$
    url: /(.*\.(json|ico|js))$
  - 
    secure: always
    static_files: build/index.html
    upload: build/index.html
    url: .*
manual_scaling: 
  instances: 1
runtime: nodejs16

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 2020-07-03
    • 1970-01-01
    • 2018-11-07
    • 2013-03-22
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多