I am Using MongoDB socket.io with nodeJs. Is there any way to clean up my imports
const express = require("express");
const app = express();
const { createServer } = require("http");
const httpServer = createServer(app);
const { Server } = require("socket.io");
const mongoose = require("mongoose");
var config = require("./config");
const url = config.mongoUrl;
Just use typescript, and then import with es6 syntax:
// tsconfig.json (at the root of your repository)
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es6"],
"allowJs": true,
"outDir": "build",
"rootDir": "src",
"strict": false,
"noImplicitAny": false,
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": "./",
},
"exclude": ["node_modules", "build"]
}
// imports src/index.ts
import express from "express";
import { createServer } from "http";
import Mongoose from "mongoose";
import { Server } from "socket.io";
import { mongoUrl as url } from "./config"
export const app = express();
const server = createServer(app);
const io = new Server(server);
If you prefer to keep the require syntax, then you could shorten your app() declaration:
const app = require('express')();