Understanding module systems is essential for TypeScript projects. Here's how they work and how to configure them.
Module Formats#
tsconfig Module Options#
ES Modules in Node.js#
Import/Export Patterns#
CommonJS Interoperability#
Path Aliases#
Barrel Files#
Module Augmentation#
Dual Package Publishing#
Best Practices#
Configuration:
✓ Use "moduleResolution": "bundler" for apps
✓ Use "moduleResolution": "node16" for Node.js
✓ Enable esModuleInterop
✓ Use verbatimModuleSyntax
Imports:
✓ Use type-only imports when possible
✓ Prefer named exports over default
✓ Use path aliases consistently
✓ Avoid deep barrel re-exports
Publishing:
✓ Ship both ESM and CJS
✓ Include type definitions
✓ Use exports field properly
✓ Test in both module systems
Conclusion#
Understanding TypeScript's module systems helps you configure projects correctly and write interoperable code. Use ES Modules for modern projects, configure esModuleInterop for CommonJS compatibility, and set up proper dual-package exports when publishing libraries.