Amazon Lambda to Firebase

前端 未结 7 1488
甜味超标
甜味超标 2020-11-30 03:42

I get \'Cannot find module \'firebase\' when I try to run this in Lambda (Node.js 4.3)

var Firebase = require(\'firebase\');

Same thing hap

7条回答
  •  忘掉有多难
    2020-11-30 03:52

    To safely use firebase npm package (version 3.3.0) in AWS Lambda (Nodejs 4.3), Please do the following:

    'use strict';
    
    var firebase = require("firebase");
    
    exports.handler = (event, context, callback) => {
        context.callbackWaitsForEmptyEventLoop = false;  //<---Important
    
        var config = {
            apiKey: "<>",
            authDomain: "<>.firebaseapp.com",
            databaseURL: "https://<>.firebaseio.com",
            storageBucket: "<>.appspot.com",
        };
    
        if(firebase.apps.length == 0) {   // <---Important!!! In lambda, it will cause double initialization.
            firebase.initializeApp(config);
        }
    
        ...
        
        ...
    };
    

提交回复
热议问题