Skip to content
Advertisement

Firebase function deploy fails with requiring external code

I am trying to re-use existing Express application and basically port it to firebase functions. I have a project structure like this:

/
  functions/
    index.js
    package.json
  src/
    app/
      index.js
  index.js

/src/app/index.js

const express = require('express')
const cors = require('cors')

const app = express()

app.use(cors({
  origin: 'http://localhost:5000',
}))

app.get('/health', (req, res) => {
  res.status(200).send('Health OK')
})

module.exports = app

/functions/index.js

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

const app = require('../src/app')

admin.initializeApp()
exports.app = functions.https.onRequest(app)

The whole set up works well when using firebase emulators:start. I can call the functions and everything works properly. However I am unable to deploy the functions as I get this error message:

Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

Functions deploy had errors with the following functions: app

When I look in Firebase console at the logs, I can’t pinpoint the exact problem:

{“@type”:”type.googleapis.com/google.cloud.audit.AuditLog”,”status”:{“code”:3,”message”:”Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging”},”authenticationInfo”:{“principalEmail”:”xxx@gmail.com”},”serviceName”:”cloudfunctions.googleapis.com”,”methodName”:”google.cloud.functions.v1.CloudFunctionsService.UpdateFunction”,”resourceName”:”projects/xxxx/locations/us-central1/functions/app”}

However when I remove this line from the /functions/index.js file:

const app = require('../src/app')

And basically put the code in /src/app/index.js it works. It seems like it’s having issues with using the code from different folder? Maybe I missed something in the documentation but do I have to specify which directories it should include?

I have express and cors dependencies in the package.json in /functions/ directory.

Advertisement

Answer

When the Firebase CLI deploys your code, it only uses files in the “functions” folder. It does not deploy anything outside of that. Your “src” folder is outside, so it doesn’t get deployed.

You will either need to move “src/app/index.js” somewhere inside the functions folder and change your require to point to it. Or you will need to somehow make a module that can be found by your package.json when it’s not running locally.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement