How would you migrate a legacy namespace-organized codebase to ES Modules, and what pitfalls commonly arise?
Short answer
Convert each namespace block into a module file with normal 'export' statements (removing the namespace wrapper syntax), replace namespace-qualified references (NamespaceName.member) with proper import statements, and watch for previously-implicit global availability (namespaces…
Advertisement
AModel answer
Convert each namespace block into a module file with normal 'export' statements (removing the namespace wrapper syntax), replace namespace-qualified references (NamespaceName.member) with proper import statements, and watch for previously-implicit global availability (namespaces often relied on being loaded via <script> tags in a specific order) that now needs explicit import/export wiring.
// Before: namespace-based (implicit global load order matters)
namespace Utils {
export function formatDate(d: Date): string { /* ... */ }
}
// After: ES Module
// utils.ts
export function formatDate(d: Date): string { /* ... */ }
// consumer.ts
import { formatDate } from './utils';Real-world example
Modernizing a legacy TypeScript codebase (originally built before ES Modules were standard) to use proper module imports/exports.
Likely follow-ups
- Why might a namespace-to-module migration also require reordering or restructuring how files are bundled/loaded?
Related topics