【问题标题】:Google Cloud Functions Build failed: function.js does not exist; Error ID: 7485c5b6Google Cloud Functions 构建失败:function.js 不存在;错误 ID:7485c5b6
【发布时间】:2021-01-06 17:09:10
【问题描述】:

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

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

不用说,该应用在我的本地机器上运行良好 (npm start)

然后我将代码推送到 Cloud Source Repositories

然后通过他们的网络应用部署到 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 的建议添加了以下内容 app.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”

【问题讨论】:

  • 如果 app.js 是您的函数的入口点,那么它应该导出一个 Firebase 函数:exports.app = functions.http.onRequest(app); 其中functions 是对需要导入您的 app.js 的 firebase-functions 的引用脚本。
  • 不,你为什么会这么认为@YeriPelona?
  • 你好@ericn 你找到解决这个问题的方法了吗?我遇到了同样的症状,我一无所知
  • 不@ImanZarrabian,我的解决方案是使用另一种技术(Springboot + Heroku)

标签: node.js express google-cloud-functions


【解决方案1】:

简而言之,在部署过程中,您必须指定要用作 GCP Cloud 函数的 JS 函数的名称,即指定 --entry-point 标志。更多信息here(请查看标志部分)。 app 在你的情况下。如果您提供部署脚本,我可以为您指出确切的位置

长答案 - 常见的误解是 GCP Cloud 功能 = Express 应用。它不是。尽管在幕后他们可能会使用 express 并且您可能会通过配置请求某些行为。实际上,对于 GCP 来说,它只是一个 JS 函数,或多或少。没有路由器或类似的东西。如果要在本地测试,请使用Functions Framework。您可以在my post获取更多详细信息

【讨论】:

  • 谢谢,但没有部署脚本。这绝对是初学者第 1 步的东西。 Functions Framework 存储库很有用。这意味着 Google Cloud Functions 的 hello world index.js 不是普通的 Javascript,它是基于 Express JS 的框架!
【解决方案2】:

像这样导出您的应用:

module.exports = { app };

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

"main": "app.js",

【讨论】:

    【解决方案3】:

    在我的例子中,我设置了由 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 查看运行时日志

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2020-11-03
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多