Firebase部署错误-找不到模块“ Firebase”

时间:2019-08-03 21:34:11

标签: firebase firebase-realtime-database google-cloud-firestore google-cloud-functions

我正在尝试部署从数据库事件触发的Google Cloud函数触发器,但是当我运行firebase deploy时,函数部署不正确,并且出现错误提示Error: Cannot find module firebase

当我第一次创建项目时,我可以部署功能而没有任何问题。自从我上一次部署任何东西已经过去了几天,但是现在我所有的函数(不仅仅是我的触发器函数)都遇到了这个问题

我的功能:

exports.deleteNotificationOnUnlike = functions
    .region("us-central1")
    .firestore.document("likes/{id}")
    .onDelete((snapshot) => {
        return db
            .doc(`/notifications/${snapshot.id}`)
            .delete()
            .catch((err) => console.error(err));
    });

Package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "busboy": "^0.3.1",
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

期望功能可以成功部署,但会收到此错误:

firebase的错误消息:

Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase'
    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/handlers/users.js:5:18)
    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)
{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"INVALID_ARGUMENT"},"authenticationInfo":{"principalEmail":"*********"},"requestMetadata":{"requestAttributes":{},"destinationAttributes":{}},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.CreateFunction","resourceName":"projects/*******/locations/us-central1/functions/createNotificationOnComment"}

我见过一些帖子,人们在其中缺少一些模块,需要在functions文件夹中运行npm i XXX,但是我还没有见过任何说Firebase模块不能被使用的实例。找到了,我不确定自上次成功部署后会发生什么变化,或者我可能缺少的模块。

3 个答案:

答案 0 :(得分:1)

找出解决方案。我没有在功能文件夹中安装firebase模块,但已将其包含在functions的父文件夹中。我进入功能并运行npm install --save firebase,部署成功。

答案 1 :(得分:0)

尝试更新 firebase npm 包,我的已使用 npm install -s firebase 解决。

-保持安全!

答案 2 :(得分:0)

确保您从功能目录而不是项目目录安装包。当我在项目目录中意外安装软件包时,我多次收到此错误,它在本地运行良好,但在部署期间创建了此类错误。所以,转到功能并从那里安装

cd functions
npm install [your package]

然后它通常工作正常