ES Module (import) Support
Overview
monSQLize supports both ES Module (import) and CommonJS (require) consumption paths.
Supported Import Styles
Style 1: CommonJS (require)
Style 2: ES Module (import)
Package.json Configuration
monSQLize exposes CJS, ESM, and TypeScript declaration entries through package exports:
Usage Examples
CommonJS Project
File: app.cjs
Run:
ES Module Project
File: app.mjs, or app.js when package.json contains "type": "module"
Run:
Import Style Comparison
Default Export
Named Exports
Mixed Usage
Available Exports
Default Export
MonSQLize(main class)
Named Exports
MonSQLize(main class)LoggerMemoryCachecreateRedisCacheAdapterTransactionManagerCacheLockManagerDistributedCacheInvalidator
Testing ES Module Support
Run Runtime Import Tests
Covered Checks
- Default export (
import MonSQLize) - Named exports (
import { Logger }) - Instance creation
- Connection and basic operations
Best Practices
1. Choose the Right Import Style
Use CommonJS when:
- Your project is a traditional CommonJS Node.js project.
- Your surrounding code still uses
require(). - The rest of your dependency graph is mostly CommonJS.
Use ES Module when:
- You are starting a modern JavaScript project.
- You want native
importsyntax. - You use TypeScript or a modern frontend/build-tool pipeline.
2. TypeScript Support
monSQLize provides TypeScript declarations:
3. Declare the Module Type in package.json
For ES Module projects:
For CommonJS projects:
Migration Guide
Migrating from CommonJS to ES Module
Step 1: Update package.json
Step 2: Update file extensions when needed
- Keep
.jsif package.json uses"type": "module". - Or rename files to
.mjs.
Step 3: Update imports
Step 4: Update exports
Step 5: Use top-level await when it fits
Notes
1. File Extensions
- ES Module: use
.mjs, or.jswhen package.json sets"type": "module". - CommonJS: use
.cjs, or.jswhen package.json sets"type": "commonjs"or omits"type".
2. __dirname and __filename
__dirname and __filename are not available in ES Module files. Use this replacement:
3. require() Is Not Available in ES Module Files
4. Dynamic Import
Compatibility Matrix
Summary
monSQLize supports
- CommonJS (
require) - traditional Node.js consumption. - ES Module (
import) - modern JavaScript consumption.
Features
- Dual-mode package exports.
- Correct package.json export mapping.
- Runtime import tests.
- TypeScript declaration output.
- Practical migration guidance.
No Changes Required for Existing Users
- Existing CommonJS projects can continue using
require(). - New projects can use ES Module imports directly.
- Both consumption styles expose the same public API surface.
Version requirement: Node.js >= 18.0.0
Recommended versions: Node.js 20.x or 22.x LTS