简体   繁体   中英

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

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

Error 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

{
  "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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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