How to convert CommonJS to ESM
Migrating a CommonJS codebase to ECMAScript modules means replacing require/module.exports with import/export and using fully specified paths that include file extensions. Conditional requires become top-level awaited dynamic imports. In package.json, set "type": "module" and use "exports" instead of "main"; drop "use strict" and replace __dirname/__filename with import.meta.dirname/filename or URL-based shims. VSCode's quick fix and `deno lint --fix` (no-sloppy-imports) automate much of the conversion, and ts2esm handles end-to-end transpilation, while older tools like cjs2esm and babel-plugin-transform-commonjs are unmaintained.
Related posts
- CommonJS is not going away
Bun mediumAdept
CommonJS remains the dominant format on npm (about 74% of popular packages in late 2022), so Bun now implements it natively as of v0.6.5 instead of transpiling it to a synchronous ESM form. CommonJS loads faster because ES modules need two passes, parsing and linking the whole graph before evaluation, to support live bindings. In Bun, requiring Babel is about 2.4x faster than importing it (85ms saved, versus 1.8x / ~60ms in Node.js), which matters for serverless cold starts. Bun supports importing CommonJS from ESM, including the de-facto `__esModule` annotation that maps `exports.default` to the default import.
- CommonJS is hurting JavaScript
Deno easyNoviceCommonJS, born in 2009 from the ServerJS effort to give server-side JavaScript a module system, became Node's default but carries core flaws: synchronous loading, poor tree-shaking, and no native browser support. ECMAScript modules fix these as a web-first standard, yet Node's decision to support both systems pushed the interoperability burden onto package authors, who must ship complex dual ESM/CJS package.json configurations. The argument is to abandon CommonJS for an all-ESM future, with tools like dnt as a stopgap for dual publishing.
- Bun v0.6.10
Bun mediumAdept
Bun v0.6.10 rewrites CommonJS module loading for better Node.js compatibility. That gets webpack working and adds `require.main`, `process.mainModule`, and runtime support for the `__esModule` annotation, which preserves default exports when CJS output from bundlers is re-imported as ESM. That behaviour is disabled when no annotation is present or the package type is "module". The release also adds `bun test` matchers (resolves/rejects, toBeArray, toBeTypeOf) and `spyOn()`, `fs.watch()`, `fs.readv`/`writev`, and more of the V8 stack trace API. It fixes `.env` parsing and a latin1 truncation bug in the WebSocket client, and adds `bun install --exact`, `--frozen-lockfile`, and a `trustedDependencies` allow-list for lifecycle scripts.
- Deno 2.1: Wasm Imports and other enhancements
Deno mediumAdeptDeno 2.1 lets Wasm modules be imported directly as ES modules. They join the module graph, are cached and type-checked, can import JS/TS modules, and no longer need read or net permissions. The release also starts the first LTS branch with six months of fixes. Other changes include `deno outdated` for JSR/npm dependency updates, embedding asset files in `deno compile` with V8 code caching (about 2x faster subsequent startups in their benchmark), stack traces in permission prompts via DENO_TRACE_PERMISSIONS, and `deno task` support for descriptions, parallel dependencies with cycle and diamond handling, and workspaces. CommonJS detection from package.json `type` improves Node compatibility.
- dnt — the easiest way to publish a hybrid npm module for ESM and CommonJS
Deno mediumAdeptdnt (Deno to Node transform) is a build tool that turns a Deno/TypeScript module into an npm package shipping ESM, CommonJS and TypeScript declarations. It rewrites module specifiers, injects shims for Deno APIs, maps remote imports to npm dependencies, type checks with tsc, and runs the test suite in both CJS and ESM under Node. A worked example builds and publishes a small module via a build_npm.ts script, then automates releases with a GitHub Actions workflow that publishes to npm on tagged releases.