Recently, I tried migrating my firebase cloud functions from javascript to typescript and split the functions into multiple files. However, I keep getting errors while trying to serve as well as deploy:
Errors while serving:
functions[functionName]: function ignored because the firestore emulator does not exist or is not running. functions[functionName]: function ignored because the firebase emulator does not exist or is not running.
Error while deploying:
functions[dataDownload(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/node_modules/fs-extra/lib/mkdirs/make-dir.js:86
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
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)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/node_modules/fs-extra/lib/mkdirs/index.js:3:44)
Tried:
Firestore/Firebase Emulator Not Running
(installed the emulators, did firebase init emulators)
unable to split Firebase functions in multiple files
Firestore local http with real db: The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error
https://github.com/firebase/functions-samples/issues/170#issuecomment-586019131
How do I structure Cloud Functions for Firebase to deploy multiple functions from multiple files?
https://javebratt.com/functions-multiple-files/
https://firebase.google.com/docs/functions/typescript#migrating_an_existing_javascript_project_to_typescript
My directory structure:
--functions
-- lib
-- node_modules
-- src
-- config
-- admin.ts
-- index.ts
-- dataDownload.ts
-- tsconfig.json
-- tslint.json
-- package.json
-- package-lock.json
--.firebaserc
-firebase.json
--package.json
--package-lock.json
The files:
index.ts:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
import { dataDownloadHandler } from './dataDownload';
export const dataDownload = functions.firestore.document('user/{uid}/download/{downloadId}').onCreate((snapshot, context) => {
dataDownloadHandler(snapshot, context);
});
dataDownload.ts:
/*
imports
*/
export const dataDownloadHandler = (snapshot:any, context:any) => {
// code
}
Nor did it work when I moved dataDownload
function into the dataDownload.ts file and did export * from './dataDownload;
in index.ts
.
admin.ts:
import * as admin from 'firebase-admin';
const auth = admin.auth();
const rtDb = admin.database();
const fsDb = admin.firestore();
const firebaseTimestamp = admin.database.ServerValue.TIMESTAMP;
const firestoreTimestamp = admin.firestore.FieldValue.serverTimestamp();
export { admin, auth, rtDb, fsDb, firebaseTimestamp, firestoreTimestamp };
package.json:
{
"name": "functions",
"engines": {
"node": "8"
},
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"watch": "tsc --watch",
"serve": "npm run build && firebase serve --only functions -P staging",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "npm run build && firebase deploy --only functions",
"logs": "firebase functions:log -P staging"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.5.0",
...other dependencies
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-plugin-promise": "^4.2.1",
"tslint": "^6.1.0",
"typescript": "^3.8.3"
},
"private": true
}
firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"hosting": [
...],
"storage": {
"rules": "storage.rules"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"database": {
"port": 9000
},
"hosting": {
"port": 5000
}
}
}
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
EDIT: https
cloud functions work; firestore
and database
cloud functions don't.