Decaf JS

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

A CoffeeScript to ES6 transpiler that leverages the CoffeeScript compiler to produce modern JavaScript syntax. Stable version 0.6.2 is available on npm. Unlike other transpilers, Decaf uses the official CoffeeScript parser to build an AST and then maps it to ES6 constructs, falling back to original CoffeeScript output for unsupported syntax. It can be used as a CLI tool or as a Node module with jscodeshift for code transforms. Development appears to be in maintenance mode with limited recent updates.

error Error: Cannot find module 'coffee-script'
cause Missing peer dependency coffee-script / coffeescript.
fix
Install: npm install coffeescript (or coffee-script for older versions).
error TypeError: decaf.compile is not a function
cause Incorrect import/require: probably using import or destructuring.
fix
Use: const decaf = require('decafjs'); decaf.compile(...);
error SyntaxError: Unexpected token while parsing CoffeeScript
cause Input contains unsupported CoffeeScript syntax or invalid input.
fix
Check source for syntax errors; Decaf falls back to original CoffeeScript output, but invalid syntax will still break.
gotcha The package name on npm is 'decafjs', not 'decaf'. Using 'npm install decaf' installs a different unrelated package.
fix Install with 'npm install decafjs'.
gotcha The exported object is the compile function itself; you cannot destructure named exports like { compile }.
fix Use: const decaf = require('decafjs'); then decaf.compile(...).
deprecated Package has not been updated since 2018; may not support latest CoffeeScript or ES6 features.
fix Consider alternatives like decaffeinate (more actively maintained).
gotcha When used as CLI globally, command is 'decaf' not 'decafjs'. Ensure global install.
fix Run: npm install -g decafjs; then decaf <file or directory>.
npm install decafjs
yarn add decafjs
pnpm add decafjs

Demonstrates basic usage: compile CoffeeScript string and file to ES6 JavaScript using Decaf.

const decaf = require('decafjs');
const path = require('path');
const fs = require('fs');

// Compile a CoffeeScript string to ES6 JS
const coffeeCode = 'square = (x) -> x * x';
const es6Code = decaf.compile(coffeeCode);
console.log(es6Code); // outputs: var square = (x) => x * x;

// Compile a file (assuming example.coffee exists)
const filePath = path.join(__dirname, 'example.coffee');
const coffeeFromFile = fs.readFileSync(filePath, 'utf8');
const jsFromFile = decaf.compile(coffeeFromFile, { tabWidth: 2 });
fs.writeFileSync('example.js', jsFromFile);
console.log('Compiled successfully!');