Recently, I integrated Firebase Functions into my project with the default settings, except for changing the value "main": "src/index.ts"
in the package.json
file because the default path was incorrect.
Here is the code that was working:
// index.ts
const { initializeApp } = require('firebase-admin/app');
const { onValueCreated } = require('firebase-functions/v2/database');
const { logger } = require('firebase-functions');
initializeApp();
exports.testFn = onValueCreated(
'/users/{uid}/company/accounts/{accId}',
(event) => {
logger.log(event);
}
);
However, after making changes to switch from require
to ESM imports and adding a type to an event parameter, the following error occured:
// index.ts
// Switched imports from require to ESM
import { initializeApp } from 'firebase-admin/app';
import { onValueCreated } from 'firebase-functions/v2/database';
import { logger } from 'firebase-functions';
initializeApp();
exports.testFn = onValueCreated(
'/users/{uid}/company/accounts/{accId}',
(event: any) => { // Added ': any' type to event parameter to avoid .ts error
logger.log(event);
}
);
The error message received is:
shutdown requested via /__/quitquitquit
!! functions: Failed to load function definition from source: FirebaseError: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
Upon investigation, it was found that both introducing ES Modules import and using types were causing this error. The presence of typescript
in package.json
did not resolve the issue. Additionally, appending a file extension (like .js
or .ts
) to the end of an ESM import did not help either.