简体   繁体   中英

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 athttps://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.

The answer from Doug Stevenson explains my problem well. I was not aware of this "limitation" (or feature?).

I ended up trying two different approaches:

1. create a symbolic link to my application ✅

In my functions/ directory I created a symbolic link to my application: ln -s../src./src and then changed the path in functions/index.js like so const app = require('../src/app') → const app = require('./src/app') .

I was able to test this via emulators suite and the function also deployed succesfully and I was able to call the deployed function(s).

2. create a local module from my application ❌

This approach did not work. I modified the package.json in /functions directory so my main application is included:

"dependencies: {
  ...
  "app": "file://.."
  ...
}

And then change /functions/index.js like so const app = require('../src/app') → const app = require('app') . This however does not solve the problem as Firebase is only uploading functions/ directory and will not upload my application.

I guess one way would be to actually create a published module of my application and then Firebase would install it. I did not do this since I did not really want to publish my application. BUt it's a way.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM