🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

cjs-to-es6

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cjs-to-es6

Convert JavaScript files from CommonJS to ES6 modules

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
1.3K
-7.03%
Maintainers
1
Weekly downloads
 
Created
Source

cjs-to-es6

CLI to convert JavaScript files from CommonJS to ES6 modules (aka ES2015 modules, aka JavaScript modules, aka import/export instead of require/module.exports), using 5to6-codemod.

Usage

npm install -g cjs-to-es6

Then:

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

Convert a single file:

cjs-to-es6 index.js

Convert many files:

cjs-to-es6 foo.js bar.js baz.js

Convert all files in a directory:

cjs-to-es6 lib/

The files are modified in-place.

Migrating from CommonJS to ES6 modules

Not all CommonJS usages have a 1-to-1 equivalent in ES6 modules. So you may have to correct some errors manually.

Use --verbose to get detailed output, or follow these general tips:

exports 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;

imports also must be at the top level

This is invalid:

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

There is no equivalent for this try/catch pattern in ES6 modules.

Keywords

es6

FAQs

Package last updated on 15 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts