6

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.

skwny
  • 2,930
  • 4
  • 25
  • 45

2 Answers2

11

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
2

I had modules in a different repository I wanted to include in my firebase functions. My solution was to use a git sparse-checkout to bring in the required folders into the \functions\ folders. I then added a script to my package.json to pull the latest code. How do I clone a subdirectory only of a Git repository?

Maybe this helps.

FakeFootball
  • 448
  • 3
  • 11