0
votes

I have simple google cloud function declared in functions/src/index.ts

import * as functions from 'firebase-functions';
import { Hero } from './hero';

var util = require('util')

export const repeat = functions.https.onCall(
    function (data, context){
        console.log(' repeat ' + util.inspect(data) + util.inspect(context));
        let aHero = new Hero('Google cloud', 50);
        return aHero;
    }
);

and Hero is declared in another file name hero.ts as follows in same folder as index.ts, that is functions/src/

export class Hero  {
    id: number;
    name: string;

    constructor( name: string, id: number) {
        this.name = name;
        this.id = id;
    }
  }

but firebase deploy fails with following

!  functions[repeat(us-central1)]: 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 './hero'
    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/index.js:4:16)
    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)

Hero is a class and not module, If I remove the hero.ts file and put the class in same index.ts, the code works. So where it needs to be included ?

1
Where is your hero.ts file compare to index.ts file location?Renaud Tarnec
I've tried your code (on macOS) and it does work. Maybe try a brand new Firebase project.Renaud Tarnec

1 Answers

0
votes

the solution was to delete node_modules from functions as well as project's directory and then issue firebase deploy from the functions directory.