I expect multiple functions in my Firebase admin project to have the same methods For ex: "generate a random alphanumeric string". So to avoid the boiler plate code, I have created a Utils folder and a utilityFunctions.ts file inside that folder. The file just contains one export function that generates a alphanumeric string. I have imported that file into one of my functions file with an import statement.
Here is my File that contains the intended global method: src/Utils/utilityFunctions.ts
export const theRandomDocId = function randomDocumentId(length: number):
String {
// the code to generate a random string
}
Here is one of my Function File: src/Compliment/addNewCompliment.ts
import * as functions from 'firebase-functions'
const admin = require('firebase-admin')
//This import statement below for the utilityFunctions file
import utilityFunctions = require('../Utils/utilityFunctions')
export const addTheNewCompliment = functions.region('asia-east2').https.onCall((complimentData,
context) => {
//generate random 11 alphanumeric ComplimentId converted to String
const randomComplimentId = utilityFunctions.theRandomDocId(28)
//There is a lot more code in this file but since its not relevant to the
//problem, I have excluded it.
}
Here is the error that is shown by vs code:
! functions[addNewCompliment(asia-east2)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '../Utils/utilityFunctions'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/lib/Compliment/addNewCompliment.js:5:26)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
I have also tried declaring the method without the export variable and accessing the method from my addNewCompliment.ts file without an import statement, the code compiles but when it is triggered the Firebase Functions log throws an error:
Unhandled error ReferenceError: randomDocumentId is not defined
Here is my package.json file
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"@google-cloud/storage": "^4.7.0",
"@types/sharp": "^0.25.0",
"child-process-promise": "^2.2.1",
"firebase-admin": "^8.9.0",
"firebase-functions": "^3.6.2",
"fs-extra": "^9.0.0",
"sharp": "^0.25.2"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2",
"firebase-functions-test": "^0.1.6"
},
"private": true
}
Is your Utils directory inside your functions directory? When deploying, only the functions directory is sent to the server, so anything that is not in that directory won't be uploaded. If that's not the issue, make sure your capitalization matches as the Cloud Functions backend has a case-sensitive filesystem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With