---
title: "How to convert CommonJS to ESM"
description: "Migrating a CommonJS codebase to ECMAScript modules means replacing require/module.exports with import/export and using fully specified paths that include file extensions."
url: "https://swe.quest/articles/how-to-convert-commonjs-to-esm-9c73f208d2b9fb43"
canonical: "https://deno.com/blog/convert-cjs-to-esm"
source: "Deno"
published: "2024-10-16"
difficulty: "medium"
kind: "How-to"
reading_minutes: 4
tags: ["javascript","refactoring"]
---

# How to convert CommonJS to ESM

A post on [Deno](https://swe.quest/sources/deno.md), published 2024-10-16, 4 min read, How-to. Original: [https://deno.com/blog/convert-cjs-to-esm](https://deno.com/blog/convert-cjs-to-esm)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Refactoring & migrations](https://swe.quest/tags/refactoring.md) (Engineering practices)

## Prerequisites

- Node.js module system ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- package.json basics

## Keywords

ESM, CommonJS, Deno, Node.js, VSCode, ts2esm, JSR, package.json exports

## Related posts

- [CommonJS is not going away](https://swe.quest/articles/commonjs-is-not-going-away-37cb3b514e9c598b.md): Bun · 2023-06-30 · Medium · 5 min read · JavaScript, Runtimes & VMs, Performance engineering
  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 is hurting JavaScript](https://swe.quest/articles/commonjs-is-hurting-javascript-f7a3d21e2d8d36ce.md): Deno · 2023-06-30 · Easy · 5 min read · JavaScript, Build systems
  CommonJS, 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.
- [Bun v0.6.10](https://swe.quest/articles/bun-v0-6-10-c9f67c1760f76c0a.md): Bun · 2023-06-26 · Medium · 8 min read · JavaScript, Runtimes & VMs, Testing, Build systems
  Bun v0.6.10 rewrites CommonJS module loading for better Node.js compatibility.
- [Deno 2.1: Wasm Imports and other enhancements](https://swe.quest/articles/deno-2-1-wasm-imports-and-other-enhancements-af4c182924b76014.md): Deno · 2024-11-21 · Medium · 17 min read · JavaScript, WebAssembly, Runtimes & VMs, Developer tools, Build systems
  Deno 2.1 lets Wasm modules be imported directly as ES modules.
- [dnt — the easiest way to publish a hybrid npm module for ESM and CommonJS](https://swe.quest/articles/dnt-the-easiest-way-to-publish-a-hybrid-npm-module-for-esm-and-commonjs-3ee76ec474a313e6.md): Deno · 2023-07-31 · Medium · 5 min read · TypeScript, JavaScript, Build systems, CI/CD
  dnt (Deno to Node transform) is a build tool that turns a Deno/TypeScript module into an npm package shipping ESM, CommonJS and TypeScript declarations.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
