🌐
GitHub
github.com › nolanlawson › cjs-to-es6
GitHub - nolanlawson/cjs-to-es6: CLI to convert CommonJS to ES6 modules (UNMAINTAINED) · GitHub
March 4, 2022 - CLI to convert JavaScript files from CommonJS to ES6 / ES2015 modules format.
Starred by 284 users
Forked by 22 users
Languages   JavaScript
🌐
npm
npmjs.com › package › cjs-to-es6
cjs-to-es6 - npm
March 4, 2022 - Convert JavaScript files from CommonJS to ES6 modules. Latest version: 2.0.1, last published: 4 years ago. Start using cjs-to-es6 in your project by running `npm i cjs-to-es6`. There are 7 other projects in the npm registry using cjs-to-es6.
      » npm install cjs-to-es6
    
Published   Mar 04, 2022
Version   2.0.1
Discussions

CommonJS to ES6 rewrite
Bleh - nobody fucking understands this shit. These comments are littered with completely wrong answers. It’s honestly fine to upgrade apps to es6 at anytime with no problem. The real problems come when you want to share a library. You only get more compatibility when you upgrade a standalone app to ESM, but with a shareable library you lose the ability to share with CJS users. If you stick w CJS for your standalone app, you will not be able to use ESM modules. Also note, next js does not support ESM and is entirely cjs, there is no choice here, it’s just cjs. More on reddit.com
🌐 r/node
46
14
August 24, 2023
How to convert common js to ES6 modules?
I know that there are many solutions to convert es6 to js. but when I was searching, I didn't find any solution converting js to es6! in my case I really need to convert this code to es6: const m3o = More on stackoverflow.com
🌐 stackoverflow.com
June 26, 2022
How to convert CommonJS require into ES6+ import?
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
How do you convert CommonJS (CJS) to ES6?
How do you convert Commonjs to ES6 modules? I want to convert it because of the numerous advantages (tree shaking, native performance, etc.) but the Babel docs only tell you how to convert it from ... More on github.com
🌐 github.com
1
2
🌐
egghead.io
egghead.io › lessons › javascript-convert-commonjs-requires-to-es6-imports
Convert CommonJS Requires to ES6 Imports | egghead.io
In this lesson we'll use cjs-to-es6 to convert CommonJS requires to ES6 imports. We'll also show how to convert over a exported default object to take a...
Published   June 6, 2017
🌐
Medium
electerious.medium.com › from-commonjs-to-es-modules-how-to-modernize-your-node-js-app-ad8cdd4fb662
From CommonJS to ES Modules: How to modernize your Node.js app
March 22, 2021 - cjs-to-es6 is a CLI that converts JavaScript files from CommonJS to ES6 Modules. It’s unmaintained, but still the best tool I could find. It successfully ported around 80% of my files. That’s fine.
🌐
Reddit
reddit.com › r/node › commonjs to es6 rewrite
r/node on Reddit: CommonJS to ES6 rewrite
August 24, 2023 -

I want to rewrite a application that uses CommonJS modules to ES6 modules in nodejs. It's a simple web app with simple stack (Express, Knex, Mocha, etc) but I will evolve it to a more complex app with more features and modules.
The thing that makes me worry is the current state of the ecosystem and support for ES6 modules in nodejs. Things like drivers for databases, testing frameworks, support libraries (ex: lodash), etc.
Have we passed the point where we can use ES6 modules in nodejs without any problems? Or should I stick with CommonJS for now?

🌐
Esnext
esnext.github.io › es6-module-transpiler
ES6 Module Transpiler
ES6 Module Transpiler is a JavaScript library for converting JavaScript files written using the ES6 draft specification module syntax for use in existing JavaScript environments. Try out the online editor demo to see it in action. ES6 modules are similar to existing module systems such as CommonJS, ...
🌐
Esnext
esnext.github.io › es6-module-transpiler › editor
ES6 Module Transpiler — Online Editor
ES6 Module Transpiler is a JavaScript library for converting JavaScript files written using the ES6 draft specification module syntax for use in existing JavaScript environments. Try out the online editor demo to see it in action. ES6 modules are similar to existing module systems such as CommonJS, ...
Find elsewhere
🌐
Code Daily
codedaily.io › tutorials › Convert-CommonJS-Requires-to-ES6-Imports
Convert CommonJS Requires to ES6 Imports
June 26, 2017 - In this lesson we'll use `cjs-to-es6` to convert CommonJS requires to ES6 imports. We'll also show how to convert over a exported default object to take advantage of named exports.
🌐
GitHub
github.com › wessberg › cjstoesm
GitHub - wessberg/cjstoesm: A tool that can transform CommonJS to ESM · GitHub
This is a tool that converts CommonJS modules into tree-shakeable ES Modules.
Starred by 420 users
Forked by 17 users
Languages   TypeScript
🌐
Stack Overflow
stackoverflow.com › questions › 68985334 › how-to-convert-commonjs-require-into-es6-import
How to convert CommonJS require into ES6+ import?
I have a line of code in my Node.js server.js file that goes like this: const serverApp = require('../dist/server/app.js').default; I need to convert it into ES6+ style, something like this: import
🌐
Medium
medium.com › lexical-labs-engineering › porting-your-javsascript-to-es6-d820520e900d
Porting your Javascript to ES6
August 1, 2015 - AMD and CommonJS have been great friends over the last few years. Proper module management is something that we’ve had crying out for and it’s great that it’s something that we can now take for granted. The new ES6 syntax is great, but easier to convert to from CommonJS:
🌐
Medium
medium.com › @rohitkumarkhatri › commonjs-to-es6-modules-to-typescript-a-practical-guide-24912bee7079
CommonJS to ES6 Modules to TypeScript: A Practical Guide
August 30, 2024 - Initially, it started as a simple scripting language but has since grown into one of the most versatile and widely-used programming languages on the web. This blog dives into the various modules and advancements in JavaScript, including CommonJS, ES6, and TypeScript, showcasing the transition with code examples.
Top answer
1 of 4
43

Working with CommonJS modules is pretty straight forward. You can only do default exports from CommonJS modules.

import packageMain from 'commonjs-package'; // Works
import { method } from 'commonjs-package'; // Errors

This means that all commonjs exports will live on the packageMain object, and you need to dot in to the packageMain object to pickup what you need.

packageMain.method1()

More info in the official nodejs docs

2 of 4
16

Since Node 13.10, there is another option, the most forward-looking one:

File an issue in the repo of the CommonJS library you'd like to use, persuading the maintainers to publish dual packages (ESM + CommonJS), using conditional exports.

For libraries written in TypeScript, generating dual packages is easy, and doesn't require Babel or rollup or any additional tools. Here's how I did it in local-iso-dt:

Relevant parts of package.json:

{
  "name": "local-iso-dt",
  "version": "3.1.0",
  "description": "...",
  "type": "commonjs",
  "exports": {
    "node": {
      "import": "./index.mjs",
      "require": "./index.js"
    },
    "default": "./index.mjs"
  },
  "main": "index.js",
  "files": [
    "index.ts",
    "index.mjs",
    "index.js"
  ],
  "scripts": {
    "clean": "rm index*.js index.mjs",
    "prepublishOnly:cjs": "tsc index.ts --esModuleInterop --removeComments",
    "prepublishOnly:esm": "tsc index.ts -t ES2015 --types node && mv index.js index.mjs",
    "prepublishOnly": "npm run prepublishOnly:esm; npm run prepublishOnly:cjs"
  },
  "devDependencies": {
    "typescript": "^4.0.2"
  },
}

prepublishOnly:esm renames the output manually because TypeScript can't yet generate .mjs output directly and --outFile doesn't work with ES Modules.

The exports block has the "conditional exports that enable TypeScript code transpiled with ES Modules, to use named imports. TypeScript doesn't directly support .mjs input files.

No tsconfig.json was necessary for this simple module.

🌐
YouTube
youtube.com › watch
Converting CommonJS to ES6 - YouTube
Learn how to smoothly transition your Nativescript project from CommonJS to ES6 module syntax, with practical examples and easy-to-follow instructions.---Thi...
Published   May 25, 2025
Views   0
🌐
npm
npmjs.com › package › commonjs-to-es-module-codemod
commonjs-to-es-module-codemod - npm
You can convert index.js and index.ts to ES Modules using jscodeshift and unpkg. # Install jscodeshift npm install --global jscodeshift # Transform using latest version LATEST_VERSION=$(npm view commonjs-to-es-module-codemod version) jscodeshift ...
      » npm install commonjs-to-es-module-codemod
    
Published   May 10, 2023
Version   0.5.7
🌐
Tsmx
tsmx.net › convert-existing-nodejs-project-from-commonjs-to-esm
Convert an existing NodeJS project from CommonJS to ESM / ECMAScript – tsmx
In this guide we’ll migrate a minimal project using popular libs and tools like Express, Jest, Supertest and ESLint from CommonJS to an ESM project. ... For the impatient here’s the straight forward uncommented list of steps to convert an existing project using Jest and ESLint from CommonJS to ESM.