简体   繁体   English

NodeJS TypeScript - Mongoose index.d.ts 抛出错误

[英]NodeJS TypeScript - Mongoose index.d.ts throwing errors

Hello so I do not know what is the problem here so when I run my NodeJS server mongoose index.d.ts throws more than one error which I do not know of I tried ignoring the node_modules from tsconfig but it seems like I am not winning您好,所以我不知道这里有什么问题,所以当我运行我的 NodeJS 服务器 mongoose index.d.ts抛出了不止一个错误,我不知道我尝试忽略tsconfig中的 node_modules 但似乎我没有获胜

Error Received: I am giving pastebin link cause trace back was long and I did not want to cut potential error: pastebin link having all error message收到错误:我正在提供 pastebin 链接,原因是追溯很长,我不想删除潜在错误: pastebin 链接包含所有错误消息

Error Header错误 Header

node_modules/@types/mongoose/index.d.ts:79:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: DocumentDefinition, FilterQuery, UpdateQuery, NativeError, Mongoose, SchemaTypes, STATES, connection, connections, models, mongo, version, CastError, ConnectionOptions, Collection, Connection, disconnected, connected, connecting, disconnecting, uninitialized, Error, QueryCursor, VirtualType, Schema, SchemaTypeOpts, Subdocument, Array, DocumentArray, Buffer, ObjectId, ObjectIdConstructor, Decimal128, Map, mquery, Aggregate, SchemaType, Promise, PromiseProvider, Model, Document, ModelUpdateOptions

My tsonfig我的 tsonfig

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/server.ts"
  ],
  "exclude": [
    "./node_modules/"
  ]
}

My base server setup:我的基础服务器设置:

import express, { Application } from 'express';
import { config } from 'dotenv';
import mongoose, { CastError, ConnectOptions } from 'mongoose';
import expressSession from 'express-session';
import MongoStore, { MongoDBStore } from 'connect-mongodb-session';
import cors from 'cors';
config();

const app: Application = express();
enum BaseUrl {
  dev = 'http://localhost:3000',
  prod = ''
}
const corsOptions = {
  origin: process.env.NODE_ENV === 'production' ? BaseUrl.prod : BaseUrl.dev,
  credentials: true
};

const mongoURI = process.env.mongoURI;
//======================================================Middleware==============================================================
app.use(cors(corsOptions));

const mongoStore = MongoStore(expressSession);

const store: MongoDBStore = new mongoStore({
  collection: 'usersession',
  uri: mongoURI,
  expires: 10 * 60 * 60 * 24 * 1000
});

const isCookieSecure: boolean = process.env.NODE_ENV === 'production' ? true : false;

app.use(
  expressSession({
    secret: process.env.session_secret,
    name: '_sid',
    resave: false,
    saveUninitialized: false,
    store: store,
    cookie: {
      httpOnly: true,
      maxAge: 10 * 60 * 60 * 24 * 1000,
      secure: isCookieSecure,
      sameSite: false
    }
  })
);

//================================================MongoDB Connection & Configs==================================================
const connectionOptions: ConnectOptions = {
  useCreateIndex: true,
  useFindAndModify: false,
  useNewUrlParser: true,
  useUnifiedTopology: true
};

mongoose.connect(mongoURI, connectionOptions, (error: CastError) => {
  if (error) {
    return console.error(error.message);
  }
  return console.log('Connection to MongoDB was successful');
});

//===============================================Server Connection & Configd====================================================
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`Server started on PORT ${PORT}`);
});

I solved the same problem by adding skipLibCheck: true option below to compilerOptions .我通过在compilerOptions下面添加skipLibCheck: true选项解决了同样的问题。

  • tsconfig tsconfig
{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/server.ts"
  ],
  "exclude": [
    "./node_modules/"
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM