Skip to content

Add Dual Support for ESM and CommonJS in Flossum #12

@pushkarscripts

Description

@pushkarscripts

Problem Description:

Currently, the package is configured as "type": "module" in package.json, which makes it ESM-only. This causes issues when other developers or tools try to require('flossum') in a CommonJS environment, resulting in the error:

❌ The package doesn't seem to have a CommonJS entry point

Goal: Update flossum to support both CommonJS (require) and ESM (import) so it can be used in any environment.


Proposed Directory Structure:

flossum/
├── src/
│   └── index.js         # Source code (ESM)
├── dist/
│   ├── index.mjs        # Output ESM build
│   └── index.cjs        # Output CJS build
├── package.json

package.json Changes:

{
  "type": "module",
  "main": "./dist/index.cjs",
  "module": "./dist/index.mjs",
  "exports": {
    "require": "./dist/index.cjs",
    "import": "./dist/index.mjs"
  },
  "bin": {
    "flossum": "./dist/index.mjs"
  },
  "scripts": {
    "build": "node build.mjs"
  }
}

Build Setup:

Use esbuild to generate both ESM and CJS builds:

npx esbuild src/index.js --bundle --platform=node --format=esm --outfile=dist/index.mjs
npx esbuild src/index.js --bundle --platform=node --format=cjs --outfile=dist/index.cjs

Optional: Automate with a build.mjs script or directly in package.json.


Resulting Usage:

import { fn } from 'flossum' — works in ESM
const { fn } = require('flossum') — works in CommonJS
✅ CLI remains intact


Test Checklist:

  • npm pack works without warning
  • require('flossum') in CJS project works
  • import in ESM project works
  • CLI (flossum) functions the same
  • npm publish --dry-run is clean

Note:

  • No need to change "type": "module" — just generate a .cjs file as fallback.
  • Avoid index.js ambiguity — always use .mjs and .cjs for clarity in dual support.
  • This setup ensures compatibility with Node.js, bundlers, and all modern tooling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIbugSomething isn't workingenhancementNew feature or requestfeaturehelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions