New to Node and Typescript. I am getting an error that mongoose.connect is not a function when I run tsc.
I have the following code:
import express = require('express');
import * as mongoose from "mongoose";
/** Routes for the app */
import apiUserRouter from "./api/user"
class App{
public express :express.Application
constructor() {
this.express = express()
this.setupDb();
}
private setupDb() : void {
var mongoDb = 'mongodb://127.0.0.1/my_database';
mongoose.connect(mongoDb);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB Connection error'));
}
}
If I change
import * as mongoose from "mongoose"
to
import mongoose = require('mongoose');
Then everything works fine.
I have run the following npm command for types as my understanding is that this should have fixed the issue.
npm install @types/mongoose --save
Edit: Adding my packages.json
{
"name": "nodejs-ts-test2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.11.1",
"@types/mongoose": "^5.0.3",
"typescript": "^2.7.2"
},
"dependencies": {
"express": "^4.16.2",
"mongoose": "^5.0.7"
}
}
and tsconfig.json:
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"outDir": "dist",
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}
Mongoose schemas are separate from TypeScript interfaces, so you need to define both a document interface and a schema until V6. 3.1. Mongoose supports auto typed schemas so you don't need to define additional typescript interface anymore but you are still able to do so.
Mongoose requires a connection to a MongoDB database. You can require() and connect to a locally hosted database with mongoose. connect() , as shown below. You can get the default Connection object with mongoose.
However, given Mongoose wraps the mongodb native driver and that does not require MongoDB installation, I would expect that you do not need to have it installed when not using localhost.
Error on initial connection. If initial connection fails, Mongoose will emit an 'error' event and the promise mongoose. connect() returns will reject. However, Mongoose will not automatically try to reconnect.
Since you didn't share your package.json or tsconfig, it was not possible to say where the error might be. So I created new project for the code you have shared such that the error does not occur. Compare the files that I am sharing with the ones you have to narrow down your problem.
The package.json
{
"name": "mong_type",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.11.1",
"@types/mongoose": "^5.0.3",
"typescript": "^2.7.2"
},
"dependencies": {
"express": "^4.16.2",
"mongoose": "^5.0.7"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src"]
}
src/app.ts
import express from "express";
import mongoose from "mongoose";
class App {
public express: express.Application;
constructor() {
this.express = express();
this.setupDb();
}
private setupDb(): void {
var mongoDb = "mongodb://127.0.0.1/my_database";
mongoose.connect(mongoDb);
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB Connection error"));
}
}
This:
import mongoose from 'mongoose'
worked for me after running:
npm install mongoose @types/mongoose --save
Way more detailed explanation on why this works, here.
The solution to this problem is to comment the below line in your tsconfig.json file found in your project's base directory, just comment this damn line
"esModuleInterop":true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With