by nolanlawson

nolanlawson / cjs-to-es6

CLI to convert CommonJS to ES6 modules (UNMAINTAINED)

207 Stars 17 Forks Last release: Not found Apache License 2.0 30 Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

cjs-to-es6 (UNMAINTAINED)

CLI to convert JavaScript files from CommonJS to ES6 modules (aka ES2015 modules, aka JavaScript modules, aka hipster


This tool uses 5to6-codemod under the hood. It's basically just a thin convenience wrapper, which can process multiple files and convert both

s and

Note that the process isn't foolproof, so you may have to manually tweak some things. But it can usually get you ~95% of the way there. See migrating below for some tips.


Install it:

npm install -g cjs-to-es6

Then run it:

cjs-to-es6 [ --verbose ] files/directories...


cjs-to-es6 index.js             # convert a single file
cjs-to-es6 lib/                 # convert all files in a directory
cjs-to-es6 foo.js bar.js lib/   # convert many files/directories

All files are modified in-place.

Example input and output

In comes CommonJS:

var flimshaw = require('flimshaw');
var twentyEightSkidoo = require('twenty-eight').skidoo;

exports.flubTheDub = 'flubTheDub'; module.exports = 'zings';

Out goes ES6 modules:

import flimshaw from 'flimshaw';
import {skidoo as twentyEightSkidoo} from 'twenty-eight';

export let flubTheDub = 'flubTheDub'; export default 'zings';

Migrating from CommonJS to ES6 modules

Not all uses of CommonJS have a 1-to-1 equivalent in ES6 modules. So you might have to correct some errors manually.


to get detailed output, or follow these general tips:

s must be at the top level

This is invalid:

if (clownShoes) {
  export default new Clown();
} else {
  export default new RespectableGentleman();

Instead do:

var result = clownShoes ? new Clown() : new RespectableGentleman();
export default result;

s also have to be at the top level

This is invalid:

try {
  import MysteryModule from 'mystery-module';
} catch (err) {
  console.log("It's a mystery!");

There is no equivalent for this

pattern in ES6 modules.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.