Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local dependencies in Cloud Functions for Firebase

When I try to deploy my functions, firebase complains about this:

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const C = require('../both/constants.js');

exports.myFunc = functions.database.ref('aRef').onCreate((event) => {
  // blah blah
}

Error: Error parsing triggers: Cannot find module '../both/constants.js'

But not this:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

// This is the invite that goes to organizations.
exports.myFunc = functions.database.ref('aRef').onCreate((event) => {
  const C = require('../both/constants.js');
}

Why is that?

Update: It would appear this happens because the function will not look for the dependency until runtime. This is of course an issue because the dependencies are still needed. I should mention my project looks like this:

both
  constants.js
functions
  index.js
  myFunc.js

Update 2 Now that I'm moving my stack entirely to Firebase functions, I got back to working on this issue again. The module needs to be published for it to work within the function. Just FYI for anyone who happens upon this psot.

like image 419
skwny Avatar asked Dec 14 '22 20:12

skwny


1 Answers

Putting your functions code anywhere other than the functions directory is not supported. Note then when you deploy it says this:

i  functions: preparing functions directory for uploading...
i  functions: packaged functions (34.06 KB) for uploading
✔  functions: functions folder uploaded successfully

It is only looking at functions and all of its sub-folders.

If I deploy code that requires code outside of functions, I also see a "module not found" error in the console.

like image 106
Doug Stevenson Avatar answered Dec 21 '22 10:12

Doug Stevenson