我刚刚按照从源部署到 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"
}
}
有什么想法吗?
我认为该教程目前有问题。
按照说明操作会导致日志中出现错误:
- /workspace/index.js:1
- import express from 'express';
- ^^^^^^
- SyntaxError: Cannot use import statement outside a 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"
}
从
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}`);
});