Google Cloud Run 部署问题

问题描述 投票:0回答:1

我刚刚按照从源部署到 Cloud Run 的link中的 google cloud run 指南进行操作。但执行时出现以下错误

gcloud run deploy

Deployment failed                                                                                                 ERROR: (gcloud.run.deploy) Revision 'mybackend-00001-ctc' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.

index.js

import express from 'express';
const app = express();

app.get('/', (req, res) => {
  const name = process.env.NAME || 'World';
  res.send(`Hello ${name}!`);
});

const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
  console.log(`helloworld: listening on port ${port}`);
});

package.json

{
  "name": "helloworld",
  "description": "Simple hello world sample in Node",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "engines": {
    "node": ">=16.0.0"
  },
  "author": "Google LLC",
  "license": "Apache-2.0",
  "dependencies": {
    "express": "^4.17.1"
  }
}

有什么想法吗?

google-cloud-platform gcloud google-cloud-run
1个回答
0
投票

我认为该教程目前有问题。

按照说明操作会导致日志中出现错误:

- /workspace/index.js:1
- import express from 'express';
- ^^^^^^
- SyntaxError: Cannot use import statement outside a module

解决方案1)在package.json中添加“type”:“module”:

{
  "name": "helloworld",
  "description": "Simple hello world sample in Node",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "engines": {
    "node": ">=16.0.0"
  },
  "author": "Google LLC",
  "license": "Apache-2.0",
  "dependencies": {
    "express": "^4.17.1"
  },
  "type" : "module"
}

解决方案 2)从 ES Module 语法转向 CommonJS 语法

import
更改为
require

// import express from 'express';

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  const name = process.env.NAME || 'World';
  res.send(`Hello ${name}!`);
});

const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
  console.log(`helloworld: listening on port ${port}`);
});
© www.soinside.com 2019 - 2024. All rights reserved.