Slip
Slip

Reputation: 949

How to import module?

I have controller homeController where i do export let index

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

In app.ts

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

tslint passed it but whe i do npm start (npm run serve), i getting the error

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

path to /home.controller is right

Upvotes: 0

Views: 407

Answers (1)

Pedro Silva
Pedro Silva

Reputation: 2815

The problem is that in your HomeController you are exporting a function, the index function. So, when you import that file you are already importing the function so

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

might work

If you want to do homeController.index

In you homeController module you should do

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

module.exports = {
    index
}

Upvotes: 1

Related Questions