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.

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
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
npm install js2coffee
yarn add js2coffee
pnpm add js2coffee

Compile JavaScript to CoffeeScript using the build method, outputting CoffeeScript code.

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'