class imported but still firebase deploy fails with Cannot find module

这一生的挚爱 提交于 2020-05-17 07:38:04

问题


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:


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



来源:https://stackoverflow.com/questions/60312958/class-imported-but-still-firebase-deploy-fails-with-cannot-find-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!