LinuxCommandLibrary

decaffeinate

Convert CoffeeScript code to JavaScript

TLDR

Convert a CoffeeScript file to JavaScript

$ decaffeinate [path/to/file.coffee]
copy

Convert a CoffeeScript v2 file to JavaScript
$ decaffeinate --use-cs2 [path/to/file.coffee]
copy

Convert require and module.exports to import and export
$ decaffeinate --use-js-modules [path/to/file.coffee]
copy

Convert a CoffeeScript, allowing named exports
$ decaffeinate --loose-js-modules [path/to/file.coffee]
copy

SYNOPSIS

decaffeinate [OPTIONS...] [FILE...]

PARAMETERS

--alias
    Prefer native class extensions when possible

--backwards
    Use legacy for own syntax

--compact
    Compact generated code

--exact
    Avoid stylistic changes, focus on semantics

--extra
    Enable non-idiomatic plugins

--functions
    Convert classes to factory functions

--import
    Use ES6 import/export statements

--indent <size>
    Set indentation size (default: 2)

--loose
    Allow loose parsing for edge cases

--native
    Use native JS objects/classes

--no-<feature>
    Disable specific transformations (e.g., --no-curly-braces, --no-implicit-return)

--process-all
    Process all files recursively

--single-quote
    Use single quotes in output

--verbose
    Enable verbose logging

-o, --output <dir>
    Specify output directory

-h, --help
    Show help

-v, --version
    Show version

DESCRIPTION

Decaffeinate is a sophisticated command-line tool that automatically converts CoffeeScript source code into readable, production-ready modern JavaScript (ES6+). Developed to facilitate migration from CoffeeScript, it handles advanced features like classes, fat arrows, comprehensions, soaks, and postscripts with semantic accuracy.

Unlike basic syntax converters, decaffeinate performs deep transformations: it rewrites loops as for...of, converts classes to ES6 classes, eliminates implicit returns where possible, and supports plugins for extras like Flow types. Output is compact and idiomatic, often requiring minimal manual tweaks.

Install via npm: npm install -g decaffeinate. It processes files or directories, writing JS to same-named files with .js extension. Ideal for legacy codebases, it preserves functionality while modernizing syntax. Supports stdin for piping and numerous flags for fine-tuning (e.g., disabling specific rewrites). Always verify output with tests, as edge cases may need adjustment.

CAVEATS

Not all CoffeeScript idioms convert perfectly; complex metaprogramming or plugins may require manual fixes. Requires Node.js ≥ 10 and npm. Test output thoroughly.

INSTALLATION

npm install -g decaffeinate
Requires Node.js.

BASIC EXAMPLE

decaffeinate input.coffee
Outputs input.js.

STDIN USAGE

cat app.coffee | decaffeinate

HISTORY

Released in 2016 by Prettier team (led by James Long). Aims to retire CoffeeScript by enabling bulk migrations. Actively maintained with 20+ plugins; v6+ targets ES2020+.

SEE ALSO

coffee(1), babel(1)

Copied to clipboard