Google Cloud Functions 构建失败:function.js 不存在;错误 ID:7485c5b6

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

我使用以下步骤生成了一个演示 Express 服务器:

  • npm install -g express-generator
  • express myExpressApp --view pug

不用说,该应用程序在我的本地计算机上运行良好(

npm start

然后我将代码推送到云源存储库

然后通过其网络应用程序部署到 Google Cloud Functions

我错过了什么?

我生成的演示应用程序的源代码:

/myExpressApp/app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

/myExpressApp/routes/index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

最终更新

我按照 Abrar Hossain 的建议添加了以下内容 应用程序.js

//...
module.exports = app;

exports.app = functions.http.onRequest(app);
module.exports = { app };

package.json

  "scripts": {
    "start": "functions-framework --target=company"
  },
  "dependencies": {
    "@google-cloud/functions-framework": "^1.7.1",

但是问题依然存在

入口点设置为“app”

node.js express google-cloud-functions
6个回答
4
投票

简而言之,在部署过程中,您必须指定要用作 GCP Cloud 函数的 JS 函数的名称,即指定

--entry-point
标志。更多信息这里(请查看标志部分)。
app
在你的情况下。如果您提供部署脚本,我可以为您指出确切的位置

长答案 - 常见的误解是 GCP Cloud 功能 = Express 应用程序。它不是。尽管在幕后他们可能会使用express,并且您可能会通过配置请求某些行为。实际上,对于 GCP 来说,它只是一个 JS 函数,仅此而已。没有路由器或类似的东西。如果您想在本地测试它,请使用Functions Framework。您可以在我的帖子

获取更多详细信息

1
投票

像这样导出您的应用程序:

module.exports = { app };

并将您的 app.js 条目添加到 package.json 中:

"main": "app.js",

1
投票

我一直从错误的目录运行部署脚本,并且发生了这种情况,因此简单的解决方法就是实际

cd
进入项目目录。

一定是一个非常愚蠢的错误,因为我在网上没有找到太多建议。但是,我想在您知道如何解释反馈之前,这有点神秘。我是 Cloud Console 的新手,有一天开始教程,第二天(重新打开网站后)回到它,打开终端进行部署,并没有注意到我在

/home/${USER}
目录中,而不是项目目录。


1
投票

我有同样的错误,问题是由于将代码部署在基本文件夹而不是项目文件夹上。要显示文件夹,请键入: ls ,然后使用以下命令移动到正确的文件夹: cd NameOfYourFolder 。重复部署说明,这应该可以工作。 另一个解决方案是检查 package.json 文件中函数的名称是否正确。


0
投票

在我的例子中,我设置了由 firebase cli 使用 Typescript 选项创建的 firebase 函数,我不仅发现了这一点,还发现了一些其他问题阻碍了其部署或执行。我让它工作的唯一方法是完全删除功能文件夹并再次运行firebase cli功能设置

firebase init functions
,但这次选择Javascript选项。

其他一些需要记住的事情:

  • 使用 CommonJS 导入而不是 ES6 例如
    const gcipCloudFunctions = require("gcip-cloud-functions")
  • 在函数文件夹中运行
    npm install
    (如果尚未运行)
  • 在部署 gcloud one 之前部署 firebase 函数
    firebase deploy --only  functions
  • 部署您的 gcloud 功能
    gcloud functions deploy <your function name> --trigger-http --runtime=nodejs14 --verbosity=debug
  • 如果部署有效但未运行,请通过运行
    gcloud functions logs read <your function name>
    或通过 GCloud UI
  • 查看运行时日志

0
投票

截至 2024 年 1 月,我最好的猜测是,“带有源代码的目录”选项有问题。将代码移至存储库并将该选项保留为空使我的函数今天可以工作。

我没有完整的证明链,但是当用terraform创建Cloud Function时,

repo_source.dir
基本确认不起作用:https://github.com/hashicorp/terraform-provider-google/issues/ 11408.

© www.soinside.com 2019 - 2024. All rights reserved.