0

FYI: I have tried the solutions here, but did not work.

I am trying to organize my firebase cloud functions into individual files. I just have one function export file for now as a test.

I have a folder called functions in my root directory with a file called helloWorld.ts and an index.ts file:

/functions/helloWorld.ts:

import * as functions from 'firebase-functions'

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info('Hello logs!', { structuredData: true })
  response.send('Hello from Firebase!')
})

/functions/index.ts:

// import all functions as needed
import { helloWorld } from './helloWorld'

export { helloWorld }

This is a Nuxt 3 app, so when I run the build command (npm run build), it compiles the overall app code (including the functions folder) into a folder for distribution. This seems to work fine.

However, after the build stage, I run firebase emulators:start and get the following error:

PS C:\Users\XXX\Desktop\work\XXX> firebase emulators:start
i  emulators: Starting emulators: functions, hosting
!  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, pubsub, storage
+  functions: Using node@16 from host.
+  functions: Using node@16 from host.
i  hosting[XXX]: Serving hosting files from: .output/public
+  hosting[XXX]: Local server: http://localhost:5000
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "C:\Users\XXX\Desktop\work\XXX\.output\server" for Cloud Functions...
+  functions[us-central1-server]: http function initialized (http://localhost:5001/XXX/us-central1/server).
i  functions: Watching "C:\Users\XXX\Desktop\work\XXX\functions" for Cloud Functions...
i  emulators: Shutting down emulators.
i  ui: Stopping Emulator UI
!  Emulator UI has exited upon receiving signal: SIGINT
i  hosting: Stopping Hosting Emulator
i  hub: Stopping emulator hub
i  logging: Stopping Logging Emulator

Error: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module
PS C:\Users\XXX\Desktop\work\XXX> npm run build

Thus, I edited the functions/package.json file to include:

"type": "module"

But, I still get the above error! Any ideas how to fix?

kissu
  • 40,416
  • 14
  • 65
  • 133
redshift
  • 4,815
  • 13
  • 75
  • 138
  • It looks like firebase emulator for cloud functions do not support typescript, unless you follow this https://stackoverflow.com/questions/71708885/cannot-use-import-statement-outside-a-module-typescript-nodejs-firebase-functio – redshift Aug 05 '22 at 10:45
  • the "do not support typescript" wording is not accurate, typescript has no run time, which means no matter what you trying to do, you need to compile it to js. It is likely that you did not compile to js first – Acid Coder Aug 07 '22 at 01:57

1 Answers1

0

I ran into issues like this trying to organize my functions as well. I had to user require to get this to work properly.

exports.[groupname] = require('<path-to-function.ts>');

Then the function is renamed like groupname-functionnameexported.

For your example it would be:

Remove:

// import all functions as needed
import { helloWorld } from './helloWorld'

export { helloWorld }

Replace with below in index.ts:

exports.testing = require('./helloWorld');

Then your function will be available as testing-helloWorld and accessible at http://localhost:5001/[your-firebase-project]/us-central1/testing-helloWorld (if emulating) otherwise at wherever the Firebase console points you.

spart3b
  • 1
  • 1