DEV Community

Layoff Atlas
Layoff Atlas

Posted on Originally published at toolsfam.com

CommonJS to ESM Converter: Free Online Module Syntax Converter — How-To Guide

CommonJS to ESM Converter: Free Online Module Syntax Converter — How-To Guide

If you are modernizing an older Node.js project, one of the first chores is converting CommonJS modules — the require() and module.exports style — into ES Module syntax (import / export). Doing it by hand is slow and error-prone, especially across dozens of files. The CommonJS to ESM Converter does the mechanical part for you: paste CommonJS code, get converted ESM output, all processed locally in your browser.

Try it free: CommonJS to ESM Converter

What the tool does

It converts common CommonJS patterns into ESM syntax where the conversion is safe:

  • const fs = require('fs') becomes import fs from 'fs'
  • const { join } = require('path') becomes import { join } from 'path'
  • module.exports = ... becomes export default ...
  • exports.foo = ... becomes export { foo } ... or export const foo = ...

The tool gives you clear output you can copy or download as a text report, plus a reset action to start over. It's ideal for quick reviews, debugging notes, code review prep, QA handoff, and documentation examples — anywhere you need to show a module pattern in modern ESM form without committing to a full migration yet.

How to use it, step by step

  1. Open the CommonJS to ESM Converter in your browser.
  2. Paste your CommonJS code into the input box — for example, a file full of require() calls and module.exports.
  3. Run the conversion. The parser rewrites the module patterns into import/export syntax.
  4. Review the findings and the converted output. The tool is labeled best-effort local analysis, so give the result a quick read before using it.
  5. Copy the output to your clipboard or download the text report, then paste it into your project file.
  6. Use the reset action to clear the input and convert the next file.

Practical tips

  • Convert one file at a time, then run your tests. The converter handles standard patterns well, but unusual code — dynamic require() calls, conditional imports, or heavily reassigned exports — may need manual attention. Run your test suite after each file.
  • Watch for __dirname and __filename. These CommonJS globals don't exist in ESM. After converting, replace them with the standard import.meta.url + fileURLToPath pattern from Node's url module.
  • Check your package.json type field. ESM files need either a .mjs extension or "type": "module" in package.json. Decide on your convention before you start converting so you don't end up with mixed module types.

FAQ

Does the converter upload my code?
No. The normal workflow runs locally in your browser — your code never leaves your device.

Is this a full replacement for a proper migration tool or CI check?
No. It's a best-effort local converter for quick, first-pass conversions. For production changes, verify the final result with your project's own compiler and CI checks.

Can I save the converted output?
Yes. After running the tool, copy the output or download it as a text report.

What patterns does it handle?
Common, standard ones: require() assignments (including destructured imports), module.exports, and exports.* assignments, converted to the matching ESM import/export forms where safe.

Privacy note

The conversion runs 100% in your browser. No account is required and your code is never uploaded to a server — safe to use with proprietary source code.

Top comments (0)