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] <files...>
decaffeinate [options] --inline '<code>'
decaffeinate [options] --stdin

PARAMETERS

--inline <code>
    Converts the provided CoffeeScript code string directly, printing the result to stdout.

--stdin
    Reads CoffeeScript input from standard input (e.g., piped from another command) and prints the converted JavaScript to stdout.

--output-dir <dir>, -o <dir>
    Specifies the directory where converted JavaScript files will be written. If not specified, files are written to the same directory as input files or printed to stdout if --print is used.

--print, -p
    Prints the converted JavaScript code to standard output instead of writing to files.

--literate
    Treats input as Literate CoffeeScript, which allows CoffeeScript code to be embedded within Markdown-like documentation.

--prefer-const
    Instructs decaffeinate to prefer using 'const' for variables that are not reassigned, improving code quality and adhering to modern JavaScript best practices.

--force-overwrite
    Overwrites existing JavaScript files in the output directory without prompting for confirmation. Use with caution.

--extension <ext>
    Specifies the output file extension for converted files (default: .js).

DESCRIPTION

decaffeinate is a command-line tool and library designed to convert CoffeeScript source code into idiomatic, modern JavaScript (ES6+). Developed primarily for migrating existing CoffeeScript projects to JavaScript, it aims to produce human-readable and maintainable output. Unlike simple transpilers that might generate less readable code, decaffeinate focuses on emitting JavaScript that closely resembles what a human developer would write, including class syntax, const/let, arrow functions, and other ES2015+ features.

It handles a wide range of CoffeeScript constructs, from simple variables and functions to complex classes, comprehensions, and destructuring assignments, providing a robust solution for modernizing legacy codebases. It is typically installed via npm or yarn as a Node.js package, making it an essential utility for large-scale code migration efforts.

CAVEATS

  • Node.js Dependency: decaffeinate is a Node.js package and requires Node.js and npm/yarn to be installed on your system. It is not a native Linux binary.
  • Conversion Fidelity: While highly accurate, complex CoffeeScript constructs or very specific coding styles might require minor manual adjustments after conversion to ensure optimal readability or correct behavior.
  • Runtime Dependencies: It does not automatically handle runtime dependencies that might be CoffeeScript-specific (e.g., requireing .coffee files directly without precompilation in a Node.js environment).

INSTALLATION

As a Node.js package, decaffeinate is typically installed globally via npm install -g decaffeinate or yarn global add decaffeinate. This makes the decaffeinate command available in your system's PATH, allowing you to run it from any directory.

INTEGRATION WITH BUILD SYSTEMS

While primarily used for one-off migration of entire codebases, decaffeinate can be integrated into build pipelines (e.g., using Gulp or custom scripts) to automate the conversion process, although this is less common than its direct use for project migration.

HISTORY

decaffeinate was created by Brian J. Brennan and first gained prominence around 2015-2016. Its primary motivation was the increasing adoption of modern JavaScript (ES6+) and the desire to migrate existing CoffeeScript projects to a more contemporary and widely supported language. As CoffeeScript's popularity waned relative to pure JavaScript, decaffeinate emerged as a crucial tool for organizations looking to reduce their dependency on CoffeeScript without a full rewrite. It has seen continuous development, evolving to support newer JavaScript features and improve conversion fidelity, becoming a go-to utility for large-scale CoffeeScript to JavaScript migrations.

SEE ALSO

coffee(1), npm(1), yarn(1), babel(1)

Copied to clipboard