ฉันกำลังพยายามปรับใช้ฟังก์ชันคลาวด์ GCP จากซอร์สโค้ด (repo git แบบมิเรอร์) ดูเหมือนว่าการปรับใช้ฟังก์ชันคลาวด์จะค้นหาดัชนี js ของฉันอย่างถูกต้อง (ซึ่งมีตัวจัดการฟังก์ชัน) แต่ล้มเหลวโดยมีข้อผิดพลาด Code in file index.js can't be loaded. Cannot find module 'express'
ที่ผู้อื่นโพสต์ไว้มากมาย ไฟล์ 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"
},
...
}
package.json ของฉันอยู่ในรูทโปรเจ็กต์ และโค้ดของฉันที่แสดงฟังก์ชันนั้นอยู่ใน /app/index.js
ฉันทำอะไรผิดที่นี่?
แก้ไข: ฉันสามารถทำซ้ำสิ่งนี้ได้ด้วยแอปที่เรียบง่ายมาก แอพมีเพียง 4 ไฟล์เท่านั้น:
app/index.js .gitignore package.json yarn.lock
// package.json
{
"name": "cloud-functions-test",
"version": "1.0.0",
"main": "app/index.js",
"repository": "[email protected]:roballsopp/cloud-functions-test.git",
"author": "rob <[email protected]>",
"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;
ฉันส่งสิ่งนี้ไปที่ GitHub ตั้งค่า repo แบบมิเรอร์ใน GCP และปรับใช้ฟังก์ชันคลาวด์ HTTP ด้วย การตั้งค่าทั้งหมดสำหรับการปรับใช้เป็นค่าเริ่มต้น ยกเว้นว่าฉันตั้งค่า "พื้นที่เก็บข้อมูล" เป็นชื่อของ repo ของฉันดังที่แสดงใน Cloud Source Repositories "ฟังก์ชันที่จะดำเนินการ" เป็น testApp
และ "ไดเรกทอรีที่มีซอร์สโค้ด" เป็น /app