js2coffee
raw JSON → 2.2.0 verified Fri May 01 auth: no javascript maintenance
JavaScript to CoffeeScript compiler. v2.2.0 (latest) rewrites the 0.x series with a new Esprima-based parser, offering better JavaScript coverage. Released on npm and Bower. Key differentiator: unidirectional conversion from JS to CoffeeScript with warnings for unrepresentable patterns.
Common errors
error Cannot find module 'js2coffee' ↓
cause Package not installed
fix
npm install js2coffee
error js2coffee is not a function ↓
cause v2.x does not have a default export
fix
Use js2coffee.build() instead of trying to call js2coffee()
error require(...).js2coffee is not a function ↓
cause 0.x API does not exist in v2
fix
Update to js2coffee.build() for v2
Warnings
breaking 2.0 release rewrites API: 0.x `js2coffee.js2coffee()` no longer works ↓
fix Use `js2coffee.build()` instead of the old `js2coffee.js2coffee()`
deprecated 0.x series is deprecated. No longer supported. ↓
fix Upgrade to v2.x using the migration guide
gotcha Not all JavaScript constructs convert cleanly; result.warnings may contain warnings for unrepresentable patterns ↓
fix Check result.warnings array after conversion and handle accordingly
Install
npm install js2coffee yarn add js2coffee pnpm add js2coffee Imports
- js2coffee wrong
import js2coffee from 'js2coffee' (ESM not supported)correctconst js2coffee = require('js2coffee') - build wrong
js2coffee(source)correctjs2coffee.build(source) - version
js2coffee.version (string)
Quickstart
const js2coffee = require('js2coffee');
const source = `
var x = 1;
function hello(name) {
console.log('Hello, ' + name);
}
hello('world');
`;
const result = js2coffee.build(source);
console.log(result.code);
// x = 1
// hello = (name) ->
// console.log 'Hello, ' + name
// hello 'world'