簡體   English   中英

NodeJS TypeScript - Mongoose index.d.ts 拋出錯誤

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

您好,所以我不知道這里有什么問題,所以當我運行我的 NodeJS 服務器 mongoose index.d.ts拋出了不止一個錯誤,我不知道我嘗試忽略tsconfig中的 node_modules 但似乎我沒有獲勝

收到錯誤:我正在提供 pastebin 鏈接,原因是追溯很長,我不想刪除潛在錯誤: pastebin 鏈接包含所有錯誤消息

錯誤 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

我的 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/"
  ]
}

我的基礎服務器設置:

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

我通過在compilerOptions下面添加skipLibCheck: true選項解決了同樣的問題。

  • 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