22

I am new to Node.js and am trying to build a node/express/mongoose server app with TypeScript.

Here is my app.ts file:

// lib/app.ts
import express from 'express';
import * as bodyParser from 'body-parser';
import { Routes } from './routes/crmRoutes';
import * as mongoose from "mongoose";

class App {
  public app: express.Application;
  public routePrv: Routes = new Routes();
  public mongoUrl: string = 'mongodb://localhost/TodosDB';

  constructor() {
    this.app = express();
    this.config();
    this.routePrv.routes(this.app);
    this.mongoSetup();
  }

  private mongoSetup(): void {
    mongoose.connect(this.mongoUrl, {
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
  }

  private config(): void {
    // support application/json type post data
    this.app.use(bodyParser.json());
    //support application/x-www-form-urlencoded post data
    this.app.use(bodyParser.urlencoded({ extended: false }));
  }
}

export default new App().app;

However, when I try to compile my application, I get:

TypeError: mongoose.connect is not a function

I've used up all my Google skill -- no luck.

Can anyone tell me what I'm doing wrong?

Nick Hodges
  • 16,902
  • 11
  • 68
  • 130
  • 1
    place the following in mongoSetup function and let me know what you'r getting ` mongoose.connect(this.mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true }).then(function(data) { console.log("data = ", data); }).catch(function(err) { console.log(err); });` – Alok Deshwal Sep 15 '19 at 16:05
  • 1
    Log your mongoose just after loading the mongoose module. – Alok Deshwal Sep 15 '19 at 16:07
  • I just copied your code and replace first line with `import * as express from 'express';` and it simple worked. – Alok Deshwal Sep 15 '19 at 16:10
  • @AlokDeshwal -- thanks for your interest. I can't log to the console because the code won't compile. Also, I replaced my import statement with what you had, and I get the same result. Didn't change anything. – Nick Hodges Sep 15 '19 at 19:32
  • I made your change, did a `tsc ./lib/server.js` and that compiled. Then it ran. Thanks for the help. – Nick Hodges Sep 15 '19 at 19:42
  • By the way I really like the way you have designed your app class. :-) – Khuram Nov 14 '21 at 15:38
  • I would like to know about the skeleton and file (MVC or other) organization of your project. Can you please share that? Thanks! :-) – Khuram Nov 14 '21 at 17:58

4 Answers4

31

Replace:

import * as mongoose from "mongoose";

With:

import mongoose from "mongoose";
Vijay Shaaruck
  • 611
  • 8
  • 5
18

This worked for me: replace

import * as mongoose from "mongoose";

with

import mongoose = require("mongoose");
sffc
  • 6,186
  • 3
  • 44
  • 68
7

if you have "esModuleInterop": true, in your tsconfig.json it forces you to import mongoose differently so change the above option or change the way you import mongoose

javad bat
  • 4,236
  • 6
  • 26
  • 44
3

A nice way is to import only the methods you need eg.

import { connect } from 'mongoose';

Then in your class

class App {
  private async mongoSetup(): Promise<void> {
    await connect(this.mongoUrl, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
  }
}
Josh Stuart
  • 1,530
  • 1
  • 13
  • 23