导入了类,但仍在Firebase部署中失败,并找不到模块 [英] class imported but still firebase deploy fails with Cannot find module
问题描述
我在functions/src/index.ts中声明了简单的Google Cloud函数
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;
}
);
和Hero在另一个文件名hero.ts中声明,如下所示,在与index.ts相同的文件夹中,即functions/src/
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;
}
}
但 firebase部署 失败,并出现以下情况
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.ts文件并将该类放在相同的index.ts中,则代码可以正常工作.那么需要在哪里包含它?
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 ?
推荐答案
解决方案是从功能和项目中删除 node_modules 的目录,然后从功能目录发出 firebase部署.
the solution was to delete node_modules from functions as well as project's directory and then issue firebase deploy from the functions directory.
这篇关于导入了类,但仍在Firebase部署中失败,并找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!