Migrate backend from CommonJS to ES Modules#2134
Migrate backend from CommonJS to ES Modules#2134trillium wants to merge 10 commits intodevelopmentfrom
Conversation
…tle caching, and commit tracking - Python skill (verify_stale_issue.py) with issue type detection - Bash implementations for single and batch issue verification - Label-based scope detection (dev vs non-dev roles) - Title consistency caching system - Git commit history analysis for PR tracking - Decision tree logic for issue verdict categorization
Convert all require() calls for third-party npm packages (express, mongoose, jsonwebtoken, etc.) to ESM import syntax as the first step of the CJS-to-ESM migration.
…ensions Convert all require() calls for relative paths (./x, ../x) to ESM import syntax, adding explicit .js file extensions as required by the ESM module resolution algorithm.
Replace all module.exports assignments with ESM export syntax. Barrel files (models/index.js, controllers/index.js, middleware/index.js, validators/index.js) use named exports; individual modules use export default.
…check Convert the CJS entry-point guard pattern to its ESM equivalent using fileURLToPath(import.meta.url), which is needed because ESM has no require.main.
…configs to .cjs Flip the ESM switch by adding "type": "module" to backend/package.json. Rename jest.config.js and jest-mongodb-config.js to .cjs since Jest config files must remain CommonJS until the Vitest migration.
Replace Jest with Vitest: add vitest.config.js with node environment and 30s timeout, update package.json scripts (test -> vitest run, test:watch -> vitest), convert jest.setup.js to use vi.mock, and remove jest/jest-mongodb config files and dependencies.
… test files Replace all Jest test APIs with their Vitest equivalents (jest.mock -> vi.mock, jest.fn -> vi.fn) and add explicit vitest imports (describe, it, expect, vi, etc.) to every test file.
…-test.js Vitest does not support the done() callback pattern; remove done parameter and done() calls from all test functions, using async/await instead. Add vitest lifecycle imports (beforeAll, afterEach, afterAll) to setup-test.js.
abc5e7a to
140919e
Compare
Code Review: Migrate backend from CommonJS to ES ModulesOverall AssessmentThis is a well-structured, methodical migration. The commit history shows a disciplined approach — third-party imports first, then relative imports, then exports, then the That said, I found a few issues ranging from potential runtime breakage to minor housekeeping. Issues Found1.
|
Fixes #2133
What changes did you make and why did you make them ?
require()calls to ESMimportstatements across 76 backend files (~160 occurrences)module.exportstoexport defaultor named exports (~55 occurrences).jsextensions to all relative imports (required by Node ESM)require.main === modulewithimport.meta.urlequivalent (5 files)__dirnamewithfileURLToPath(import.meta.url)(1 file)"type": "module"to backend/package.jsonjest.mock/jest.fn/jest.spyOnto Vitest equivalents (vi.mock/vi.fn/vi.spyOn)Note: This PR must be merged before #2137 (test suite fixes), which depends on the ESM/Vitest changes made here.
Screenshots of Proposed Changes Of The Website (if any, please do not screen shot code changes)
N/A — no visual changes, backend only.