CoffeeScript Compiler
CoffeeScript is a 'little language' that transpiles into JavaScript, aiming to provide a more concise and readable syntax inspired by Ruby. Its current stable version is 2.7.0. While once very popular, it has seen reduced adoption since the widespread introduction of ES6+ features, which brought much of CoffeeScript's syntactic sugar directly into JavaScript. The project is primarily in maintenance mode with infrequent but significant updates, targeting modern JavaScript. Key differentiators include its significant whitespace, implicit returns, and a streamlined object/array literal syntax compared to direct JavaScript. CoffeeScript maintains backward compatibility where possible while progressively aligning its output with modern JavaScript standards and targeting ES2015+. It is mainly used as a development dependency for projects that chose CoffeeScript for their source code.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax to load the `coffeescript` package in a CommonJS context (e.g., an old Node.js script without `"type": "module"` in `package.json` or an `.mjs` extension) or vice-versa.fixIf in a CommonJS environment, use `const CoffeeScript = require('coffeescript');`. If in an ESM environment, ensure your `package.json` has `"type": "module"` or your file ends in `.mjs`, and use `import { compile } from 'coffeescript';` or `import * as CoffeeScript from 'coffeescript';`. -
Error: Cannot find module 'coffeescript'
cause The `coffeescript` package is not installed or not resolvable in the current environment.fixInstall the package locally with `npm install --save-dev coffeescript` for project-specific use, or globally with `npm install --global coffeescript` if using the `coffee` CLI tool system-wide. Verify the installation path and Node.js module resolution. -
TypeError: (intermediate value).compile is not a function
cause This typically occurs when the `coffeescript` module is imported incorrectly, resulting in `undefined` or an unexpected value when attempting to access the `compile` method. This could be due to incorrect default vs. named import, or CJS/ESM interop issues.fixFor CommonJS, use `const CoffeeScript = require('coffeescript');` and then `CoffeeScript.compile(...)`. For ESM, use `import { compile } from 'coffeescript';` if available, or `import * as CoffeeScript from 'coffeescript';` and then `CoffeeScript.compile(...)`. Verify the specific export structure of the installed version. -
CoffeeScript 1.x for...in loops iterate over values, but CoffeeScript 2.x for...in loops iterate over keys.
cause This is a deliberate breaking change introduced in CoffeeScript 2.x to align `for...in` behavior with JavaScript. Users upgrading from 1.x might expect the old value-iteration behavior.fixFor iterating over *values* in CoffeeScript 2.x, use `for value of array` (for arrays) or `for key, value of object` (for objects). Reserve `for key in object` for iterating over keys. Adjust existing 1.x code accordingly.
Warnings
- breaking CoffeeScript 2.x introduced significant changes in its output and some syntax semantics compared to 1.x, aiming to align with modern JavaScript (ES2015+). This includes changes to how `for...in` loops behave (now iterating keys, not values), `super` calls, implicit returns, and strict mode output. Migration from 1.x to 2.x may require adjustments.
- gotcha Despite its compiler producing modern JavaScript, integrating CoffeeScript into contemporary JavaScript build toolchains (like Webpack, Rollup, Vite, or even Babel for further transpilation) can be challenging without specific loaders or plugins. The `.coffee` extension is not natively understood by most JS tools.
- gotcha CoffeeScript is in maintenance mode with slower development compared to its peak popularity. While still functional, relying on it for new projects might lead to challenges in finding extensive community support, up-to-date tooling integrations, or rapid feature development that keeps pace with JavaScript.
- breaking CoffeeScript 2.3 introduced output that uses object spread syntax (`...`) in object literals, which was not universally supported in all browsers (e.g., Edge, Safari) or environments (e.g., React Native's JS engine) at the time of its release. This could lead to runtime errors if the compiled output was not further transpiled.
Install
-
npm install coffeescript -
yarn add coffeescript -
pnpm add coffeescript
Imports
- CoffeeScript
import CoffeeScript from 'coffeescript';
import * as CoffeeScript from 'coffeescript';
- compile
const { compile } = require('coffeescript');import { compile } from 'coffeescript'; - CompileOptions
import type { CompileOptions } from 'coffeescript'; - CoffeeScript (CJS)
import CoffeeScript from 'coffeescript';
const CoffeeScript = require('coffeescript');
Quickstart
import { compile } from 'coffeescript';
const coffeeCode = `
# A simple CoffeeScript example
square = (x) -> x * x
numbers = [1, 2, 3]
for num in numbers
console.log "The square of #{num} is #{square num}"
class Animal
constructor: (@name) ->
move: (meters) ->
console.log @name, "moved #{meters}m."
cat = new Animal "Cat"
cat.move 5
`;
try {
const jsCode = compile(coffeeCode, { bare: true });
console.log('--- CoffeeScript Input ---');
console.log(coffeeCode);
console.log('\n--- Compiled JavaScript Output ---');
console.log(jsCode);
} catch (error) {
console.error('Compilation Error:', error.message);
}