Why am I getting "mongoose.connect is not a function" when trying to connect with mongoose?
Asked Answered
N

6

24

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?

Nyctophobia answered 15/9, 2019 at 13:54 Comment(7)
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); });`Turnspit
Log your mongoose just after loading the mongoose module.Turnspit
I just copied your code and replace first line with import * as express from 'express'; and it simple worked.Turnspit
@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.Nyctophobia
I made your change, did a tsc ./lib/server.js and that compiled. Then it ran. Thanks for the help.Nyctophobia
By the way I really like the way you have designed your app class. :-)Links
I would like to know about the skeleton and file (MVC or other) organization of your project. Can you please share that? Thanks! :-)Links
M
30

Replace:

import * as mongoose from "mongoose";

With:

import mongoose from "mongoose";
Malachite answered 9/8, 2020 at 8:39 Comment(1)
https://mcmap.net/q/86513/-understanding-esmoduleinterop-in-tsconfig-file - explanationRemillard
S
17

This worked for me: replace

import * as mongoose from "mongoose";

with

import mongoose = require("mongoose");
Sideband answered 26/12, 2019 at 21:52 Comment(0)
P
6

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

Paranoiac answered 24/4, 2020 at 17:38 Comment(0)
A
2

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
    });
  }
}
Arhna answered 6/3, 2021 at 2:58 Comment(0)
P
0

if anyone still get this error, try run it in server like Express i learned it the hard way after spend almost 8 hours try every ways posible on Google

Primaveras answered 11/9, 2023 at 11:36 Comment(1)
I was porting a Next app to React and had this silly problem hahaChiffonier
T
-1

I encountered the same error in Next.js 14.i was invoking a Server Action function to execute a MongoDB query when I received this error: TypeError: mongoose.connect is not a function.I later realized that the root of this issue was my omission of the use server directive in my code.

code(server-Action):

"use server"
//code here
Thomas answered 17/3 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.