Build and Run Instructions
Why Two Module Formats?
MagicLogger provides both ESM (.mjs
) and CommonJS (.js
) entry points to support:
- ✅ Modern environments using import statements
- ✅ Legacy environments using require()
- ✅ Hybrid codebases migrating from CommonJS to ESM
- ✅ TypeScript projects with different module resolutions
Use whichever format fits your stack — the library supports both seamlessly.
Browser Support
MagicLogger works in browser-based projects (like React or Vue) with automatic adaptation:
- Terminal-specific features (e.g., cursor movement) are disabled
- ANSI styling is adjusted for browser console support
- File logging is disabled in browser contexts
- The environment is auto-detected and fallback-safe
Getting Started
- Install dependencies:
npm install
- Build the library:
npm run build
This compiles the src
files into dist/
with both ESM and CJS outputs.
Development Mode
Watch mode for rapid iteration:
npm run dev
This runs tsup
in watch mode to rebuild on file changes.
Running the Demo
You can run examples in multiple ways:
ES Modules (Recommended for Modern Node)
node dist/examples/demo.mjs
CommonJS
node dist/examples/demo.js
Without Building (TS Direct)
npx ts-node examples/demo.ts
Testing
Run all tests:
npm test
Run with coverage:
npm run test:coverage
Generate coverage badge and markdown report:
npm run test:coverage:badge
Linting and Formatting
Format code:
npm run format
Lint code:
npm run lint
Auto-fix lint issues:
npm run lint:fix
Preflight Check (Before Releasing)
Run a full validation of the build before tagging or publishing:
npm run preflight
This will:
- Clean build artifacts
- Run format checks and lint
- Run all tests
- Build the output
- Generate a coverage badge
- Inject build size stats into
README.md
Manual Release Workflow
- Run a preflight check:
npm run preflight
- Bump version and generate changelog:
npm run version:bump patch # or minor / major
- Push and publish:
git push && git push --tags npm publish
Or run it all in one step:
npm run release:manual patch
This combines preflight
+ version bump automation + changelog + README injection.
Troubleshooting
Common ESM/CJS Issues
-
Make sure you're using the correct file extensions:
.mjs
for ES Modules.js
for CommonJS
-
For older Node.js versions, fall back to CommonJS:
node dist/index.js
- If you see errors related to file extensions in
import
paths, make sure you're using Node 14+ with"type": "module"
inpackage.json
.
Package Structure
src/
— Source TypeScript filesdist/index.js
— CommonJS builddist/index.mjs
— ESM builddist/index.d.ts
— TypeScript typesexamples/
— Example/demo scriptsscripts/
— Automation scripts (coverage, versioning, build analysis)tests/
— Unit test filesdocs/
— Developer documentation