如何导入模块?

时间:2018-12-30 20:40:54

标签: node.js typescript

我有控制器homeController,我在其中export let index

import { Request, Response } from "express";
  export let index = (req: Request, res: Response) => {
     console.log("home");
 };

在app.ts中

  import * as homeController from "../src/modules/home/controllers/home.controller";
  const app = express();
  ...
   app.get("/", homeController.index);

tslint传递了它,但是当我执行npm start(npm运行发球)时,我得到了错误

 internal/modules/cjs/loader.js:589
  throw err;
   ^
 Error: Cannot find module '../src/modules/home/controllers/home.controller'

到/home.controller的路径正确

1 个答案:

答案 0 :(得分:0)

问题是在HomeController中,您正在导出一个函数,即索引函数。因此,当您导入该文件时,您已经在导入该功能了

import * as homeController from "../src/modules/home/controllers/home.controller";
  const app = express();
  ...
   app.get("/", homeController);

可能会工作

如果要执行homeController.index

您应该在homeController模块中

import { Request, Response } from "express";
let index = (req: Request, res: Response) => {
 console.log("home");
};

module.exports = {
    index
}