CodeWithDanDockerServices icon indicating copy to clipboard operation
CodeWithDanDockerServices copied to clipboard

Convert entire codebase to TypeScript with ES modules

Open Copilot opened this issue 10 months ago • 0 comments

User reported the copilot/upgrade-packages-and-modernize branch still used CommonJS (require()/module.exports) instead of ES modules, and files were still .js instead of .ts despite having TypeScript infrastructure.

Changes

Configuration

  • Added "type": "module" to package.json
  • Updated main entry point: server.js → dist/server.js
  • Changed tsconfig.json moduleResolution: "node" → "bundler" for Node.js ES modules
  • Updated build scripts to compile TypeScript before running

Module System Conversion (16 files)

  • All require() → import
  • All module.exports → export default
  • Added .js extensions to relative imports (required for Node.js ES modules)
  • Converted all source files from .js to .ts

Type Safety

  • Applied proper Mongoose generics: Schema<IFeature>, model<IFeature>()
  • Typed all Express handlers with Request, Response, NextFunction
  • Updated types.ts interfaces to match actual database schemas
  • Added global.d.ts for untyped package declarations

Docker

  • Updated production Dockerfile to build TypeScript during image creation
  • Changed entrypoint: node server.js → node dist/server.js

Example

Before:

'use strict';
const mongoose = require('mongoose');
const seeder = require('./dataSeeder');

const database = {
    async init(config) {
        await mongoose.connect(connStr);
        return mongoose.connection;
    }
};

module.exports = database;

After:

import mongoose, { Connection } from 'mongoose';
import seeder from './dataSeeder.js';
import type { DatabaseConfig } from '../types.js';

const database = {
    async init(config: DatabaseConfig): Promise<Connection> {
        await mongoose.connect(connStr);
        return mongoose.connection;
    }
};

export default database;

Files remain in repository for backward compatibility reference. Original .js files can be removed after testing.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Jan 15 '26 01:01 Copilot