Home > OS >  TypeScript Not Recognising Exported Firebase Cloud Functions
TypeScript Not Recognising Exported Firebase Cloud Functions

Time:12-11

Problem Intro

I have over a hundred Firebase Cloud Functions, and to keep the code organised, I split them into separate files per function (e.g., userFunctions, adminFunctions, authFunctions, ...) as per the instructions in the official Firebase thread.

In my index.ts I import all the different function files as:

import * as adminFunctions from './modules/adminFunctions';
import * as userFunctions from './modules/userFunctions';
...

exports.adminFunctions = adminFunctions;
exports.userFunctions = userFunctions;
...

In my userFunctions.ts file, I would declare the individual functions, some of which would call additional reusable functions from authFunctions.ts

userFunctions.ts

import { https } from 'firebase-functions';
import { performAppCheckAuthentication } from './supportingFunctions/authFunctions';

exports.deleteExpiredOrganisationMembershipInvite = https.onCall(async (data, context) => {
    // Perform AppCheck authentication
    performAppCheckAuthentication(data, context)

    // More code
    ...
})

The cross-referenced authFunctions.ts would look like this:

exports.performAppCheckAuthentication = function (
    data: { [key: string]: any },
    context: CallableContext
) {
    
    return true; // There would be actual logic here in the real code
}


Exact Issue

When I have TypeScript try to compile this code, it gives me the following error in the userFunctions.ts file in the import statement:

Module '"./supportingFunctions/authFunctions"' has no exported member 'performAppCheckAuthentication'.

How can I keep my code split into different files to retain maintainability, but also get around this issue of not being able to import the functions?

CodePudding user response:

You probably want to use the export statement instead of the exports global:

export function performAppCheckAuthentication(
    data: { [key: string]: any },
    context: CallableContext
) {
    
    return true; // There would be actual logic here in the real code
}

export const deleteExpiredOrganisationMembershipInvite = https.onCall(async (data, context) => {
    // Perform AppCheck authentication
    performAppCheckAuthentication(data, context)

    // More code
    ...
})

Docs

  • Related