【问题标题】:Mongoose Import for TypeScript Doesn't Work用于 TypeScript 的 Mongoose 导入不起作用
【发布时间】:2018-08-06 16:25:31
【问题描述】:

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
    }
}

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案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"));
      }
    }
    

    【讨论】:

    • 感谢您到目前为止的帮助,看来快速导入现在有效。但是猫鼬仍然没有。当我运行 nodemon 时,出现以下错误:“mongoose.connect”不是函数。
    • 嘿查克,是的,抱歉我没有测试。我已经更新了导入猫鼬的代码,这样您就不会收到该错误。
    • 谢谢你。我会奖励答案,但是,为什么'import * as mongoose from "mongoose"' 不起作用?它适用于我项目中的其他页面。
    • 那是因为我在 tsconfig 中使用了规则 "esModuleInterop": true 。如果您删除该规则,您将能够使用“import *”语法导入 express 和 mongoose。
    【解决方案2】:

    这个:

    import mongoose from 'mongoose'
    

    跑步后为我工作:

    npm install mongoose @types/mongoose --save
    

    更详细的解释为什么会这样,here

    【讨论】:

    • 仅供参考deprecated @types/mongoose@5.11.97: Mongoose publishes its own types, so you do not need to install this package.
    【解决方案3】:

    解决这个问题的方法是在你的项目基础目录中找到的 tsconfig.json 文件中注释以下行,只需注释这该死的行

    "esModuleInterop":true
    

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 2019-11-14
      • 2017-03-11
      • 2019-02-23
      • 2022-11-10
      • 2021-04-15
      • 2018-02-21
      • 1970-01-01
      • 2020-04-14
      相关资源
      最近更新 更多