用于TypeScript的Mongoose导入不起作用

时间:2018-02-27 05:20:31

标签: node.js typescript mongoose

Node和Typescript新手。我收到一个错误,当我运行tsc时,mongoose.connect不是一个函数。

我有以下代码:

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'));
    }
}

如果我改变

import * as mongoose from "mongoose"

import mongoose = require('mongoose');

然后一切正常。

我已经为类型运行了以下npm命令,因为我的理解是这应该已经修复了问题。

npm install @types/mongoose --save

编辑:添加我的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"
    }
}

和tsconfig.json:

{
    "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "outDir": "dist",
        "strict": true,
        "noImplicitAny": false,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true
    }
}

3 个答案:

答案 0 :(得分:2)

此:

import mongoose from 'mongoose'

跑步后为我工作:

npm install mongoose @types/mongoose --save

here对此进行了更详细的解释。

答案 1 :(得分:1)

由于您没有共享package.json或tsconfig,因此无法确定错误的位置。因此,我为您共享的代码创建了新项目,以便不会发生错误。将我分享的文件与您所拥有的文件进行比较,以缩小您的问题范围。

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"));
  }
}

答案 2 :(得分:0)

此问题的解决方案是在项目的基本目录中的tsconfig.json文件中注释以下行,只需注释该死的行

"esModuleInterop":true