Cloud functions deployment error: Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded

Cloud functions deployment error: Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded

我正在尝试添加 Angular SSR,并且我创建了以下云函数:

import * as functions from 'firebase-functions';
import * as path from 'path';

const universal = require(path.resolve(__dirname, '../../dist/server/main')).app;

export const ssr = functions.https.onRequest(universal);

package.json

{
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@types/jsdom": "^11.0.4",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.0",
    "path": "^0.12.7",
  },
  "devDependencies": {
    "typescript": "^3.8.3",
    "firebase-functions-test": "^0.2.1"
  }
}

当我构建然后部署我的函数时出现错误:

!  functions[ssr(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '/dist/server/main'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/srv/lib/ssr.js:5:19)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)

我的文件夹结构是:

dist
 |-server
 |-browser
functions
 |-lib

我该如何解决?

我猜您的代码正在尝试访问 "functions" 文件夹之外的文件。也就是说“../../dist/server/main”正在向上延伸并超出该文件夹的边界。只有 functions 文件夹的内容由 Firebase CLI 部署。部署后,该函数将无法访问除此之外的所有其他内容。如果您希望该内容可用,则必须将其复制到那里。是的,这可能意味着如果您在前端和后端都使用该文件,则必须复制该文件。