简体   繁体   English

GCP Cloud Functions 在我的 package.json 中“找不到模块‘express’”

[英]GCP Cloud Functions "Cannot find module 'express'" when it IS in my package.json

I'm trying to deploy a GCP cloud function from source code (a mirrored git repo).我正在尝试从源代码(镜像的 git 存储库)部署 GCP 云功能。 Cloud Function deploy seems to be finding my index.js properly (which contains the function handler), but fails with the Code in file index.js can't be loaded. Cannot find module 'express' Cloud Function deploy 似乎正确地找到了我的 index.js(其中包含函数处理程序),但由于Code in file index.js can't be loaded. Cannot find module 'express'Code in file index.js can't be loaded. Cannot find module 'express'而失败Code in file index.js can't be loaded. Cannot find module 'express' Code in file index.js can't be loaded. Cannot find module 'express' error that many others have posted about. Code in file index.js can't be loaded. Cannot find module 'express'许多其他人发布的Code in file index.js can't be loaded. Cannot find module 'express'错误。 My package.json file definitely contains express, however:我的 package.json 文件肯定包含 express,但是:

{
  ...
  "dependencies": {
    "@google-cloud/speech": "^2.3.1",
    "@google-cloud/storage": "^2.5.0",
    "@sentry/node": "^5.4.3",
    "cors": "^2.8.5",
    "express": "4.17.1",
    "googleapis": "45.0.0",
    "moment": "^2.24.0",
    "stripe": "^7.1.0"
  },
  ...
}

My package.json is in the project root, and my code exposing the function itself is in /app/index.js .我的 package.json 位于项目根目录中,而我公开函数本身的代码位于/app/index.js What could I be doing wrong here?我在这里做错了什么?

Edit: I was able to reproduce this with a very simple app.编辑:我能够用一个非常简单的应用程序重现这个。 The app only contains 4 files:该应用程序仅包含 4 个文件:

app/index.js
.gitignore
package.json
yarn.lock
// package.json
{
  "name": "cloud-functions-test",
  "version": "1.0.0",
  "main": "app/index.js",
  "repository": "git@github.com:roballsopp/cloud-functions-test.git",
  "author": "rob <rca06d@my.fsu.edu>",
  "license": "MIT",
  "dependencies": {
    "express": "^4.17.1"
  }
}

// index.js
const express = require('express');

const app = express();
app.get('/health', function (req, res) {
    res.status(200).send({ message: 'All good!' });
});

exports.testApp = app;

I pushed this to github, set up a mirrored repo in GCP, and deployed an HTTP cloud function with it.我把它推送到 github,在 GCP 中设置了一个镜像仓库,并用它部署了一个 HTTP 云函数。 All settings for the deployment are default, except I set "Repository" to the name of my repo as shown in Cloud Source Repositories, "Function to execute" to testApp , and "Directory with source code" to /app .部署的所有设置都是默认设置,除了我将“Repository”设置为我的存储库名称(如 Cloud Source Repositories 中所示)、“Function to execute”设置为testApp以及“Directory with source code”设置为/app

When we deploy a Cloud Function from data contained in a Git repo, we have the opportunity to specify the directory with source code .当我们从包含在 Git 存储库中的数据部署 Cloud Functions 时,我们有机会使用源代码指定目录 It is believed that this causes the deployment of that directory (and likely subordinate directories).相信这会导致部署该目录(以及可能的从属目录)。 In this story, where we have:在这个故事中,我们有:

app
  index.js
package.json

and we specified the "directory with source" as /app then what was actually deployed did not include the package.json .我们将“带有源的目录”指定为/app那么实际部署的内容包括package.json With no package.json there was no explicit dependencies and hence no knowledge of a dependency on express .没有package.json没有明确的依赖关系,因此不知道对express的依赖。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM