es6-module-crosspiler

raw JSON →
2.0.1 verified Fri May 01 auth: no javascript maintenance

An ES6 and CommonJS cross-compatible transpiler that enables mixed usage of ES6 modules and CommonJS modules during migration. Version 2.0.1 is the latest stable release but appears to be in maintenance mode as the project has seen no recent updates and uses legacy dependencies like recast and esprima-fb. Unlike higher-level transpilers (e.g., Babel), this is a lower-level library that operates on recast ASTs, allowing flexibility in custom build pipelines but lacking file system resolution and custom formatters.

error SyntaxError: Unexpected token 'import'
cause The JavaScript parser does not support ES6 module syntax (requires esprima-fb or similar).
fix
Use a custom esprima that supports ES6 modules, e.g., 'esprima-fb', when calling recast.parse().
error The module was imported incorrectly, e.g., using named import instead of default import.
cause Attempting 'import { Module } from ...' instead of 'import Module from ...'
fix
Use default import: import Module from 'es6-module-crosspiler'
breaking The library depends on 'esprima-fb', which is deprecated and may not support recent ES6 features.
fix Use a more modern transpiler like Babel or TypeScript for ES6 module transpilation.
gotcha The default export handling differs from standard ES6 module semantics: CommonJS modules imported from ES6 modules are treated as default exports without the .default property.
fix When cross-compiling, ensure that CommonJS modules do not export a .default property, as it will be overridden.
deprecated The package has not been updated since 2015 and uses deprecated dependencies like 'recast' and 'esprima-fb'.
fix Migrate to a maintained transpiler such as Babel or esbuild.
gotcha The library requires explicit metadata for each dependency (.set() or constructor), otherwise it defaults to { type: 'module', default: false }.
fix Always provide dependency metadata to avoid incorrect transpilation.
npm install es6-module-crosspiler
yarn add es6-module-crosspiler
pnpm add es6-module-crosspiler

This shows how to transpile a simple ES6 import statement to CommonJS using recast, esprima-fb, and the crosspiler.

var recast = require('recast');
var esprima = require('esprima-fb');
var Module = require('es6-module-crosspiler');

var code = 'import { foo } from \'./lib\';';
var ast = recast.parse(code, { esprima: esprima });

var m = new Module(ast, {
  dependencies: {
    './lib': { type: 'module', default: false }
  }
});

var transformedAst = m.transform();
var result = recast.print(transformedAst);
console.log(result.code);