To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
This will make the and commands available globally.
If you are using CoffeeScript in a project, you should install it locally for that project so that the version of CoffeeScript is tracked as one of your project’s dependencies. Within that project’s folder:
The and commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.
If you plan to use the option (see Transpilation) you will need to also install either globally or locally, depending on whether you are running a globally or locally installed version of CoffeeScript.
Once installed, you should have access to the command, which can execute scripts, compile files into , and provide an interactive REPL. The command takes the following options:
|Launch an interactive CoffeeScript session to try short snippets. Identical to calling with no arguments.|
|Watch files for changes, rerunning the specified command when any file is updated.|
|Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over stdio, or using some sort of extension-less file name.|
|Compile and print a little snippet of CoffeeScript directly from the command line. For example:|
|the given module before starting the REPL or evaluating the code given with the flag.|
|Suppress the “Generated by CoffeeScript” header.|
|The executable has some useful options you can set, such as , , , and . Use this flag to forward options directly to Node.js. To pass multiple flags, use multiple times.|
|Instead of parsing the CoffeeScript, just lex it, and print out the token stream. Used for debugging the compiler.|
|Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree. Used for debugging the compiler.|
- Compile a directory tree of files in into a parallel tree of files in :
- Watch a file for changes, and recompile it every time the file is saved:
- Concatenate a list of files into a single script:
- Print out the compiled JS from a one-liner:
- All together now, watch and recompile an entire project as you work on it:
- Start the CoffeeScript REPL ( to exit, for multi-line):
To use , see Transpilation.
If you’d like to use Node.js’ CommonJS to CoffeeScript files, e.g. , you must first “register” CoffeeScript as an extension:
If you want to use the compiler’s API, for example to make an app that compiles strings of CoffeeScript on the fly, you can the full module:
The method has the signature where is a string of CoffeeScript code, and the optional is an object with some or all of the following properties:
- , boolean: if true, a source map will be generated; and instead of returning a string, will return an object of the form .
- , boolean: if true, output the source map as a base64-encoded string in a comment at the bottom.
- , string: the filename to use for the source map. It can include a path (relative or absolute).
- , boolean: if true, output without the top-level function safety wrapper.
- , boolean: if true, output the header.
- , object: if set, this must be an object with the options to pass to Babel. See Transpilation.
From the root of your project:
Transpiling with the CoffeeScript compiler
To make things easy, CoffeeScript has built-in support for the popular Babel transpiler. You can use it via the command-line option or the Node API option. To use either, must be installed in your project:
Or if you’re running the command outside of a project folder, using a globally-installed module, needs to be installed globally:
By default, Babel doesn’t do anything—it doesn’t make assumptions about what you want to transpile to. You need to provide it with a configuration so that it knows what to do. One way to do this is by creating a file in the folder containing the files you’re compiling, or in any parent folder up the path above those files. (Babel supports other ways, too.) A minimal file would be just . This implies that you have installed :
See Babel’s website to learn about presets and plugins and the multitude of options you have. Another preset you might need is if you’re using JSX with React (JSX can also be used with other frameworks).
Once you have and (or other presets or plugins) installed, and a file (or other equivalent) in place, you can use to pipe CoffeeScript’s output through Babel using the options you’ve saved.
If you’re using CoffeeScript via the Node API, where you call with a string to be compiled and an object, the key of the object should be the Babel options:
First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code. You don’t need to use semicolons to terminate expressions, ending the line will do just as well (although semicolons can still be used to fit multiple expressions onto a single line). Instead of using curly braces to surround blocks of code in functions, if-statements, switch, and try/catch, use indentation.
You don’t need to use parentheses to invoke a function if you’re passing arguments. The implicit call wraps forward to the end of the line or block expression.
Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this:
Functions may also have default values for arguments, which will be used if the incoming argument is missing ().
Multiline strings are allowed in CoffeeScript. Lines are joined by a single space unless they end with a backslash. Indentation is ignored.
Block strings, delimited by or , can be used to hold formatted or indentation-sensitive text (or, if you just don’t feel like escaping quotes and apostrophes). The indentation level that begins the block is maintained throughout, so you can keep it all aligned with the body of your code.
Double-quoted block strings, like other double-quoted strings, allow interpolation.
Objects and Arrays
CoffeeScript has a shortcut for creating objects when you want the key to be set with a variable of the same name.
Lexical Scoping and Variable Safety
The CoffeeScript compiler takes care to make sure that all of your variables are properly declared within lexical scope — you never need to write yourself.
Notice how all of the variable declarations have been pushed up to the top of the closest scope, the first time they appear. is not redeclared within the inner function, because it’s already in scope; within the function, on the other hand, should not be able to change the value of the external variable of the same name, and therefore has a declaration of its own.
Because you don’t have direct access to the keyword, it’s impossible to shadow an outer variable on purpose, you may only refer to it. So be careful that you’re not reusing the name of an external variable accidentally, if you’re writing a deeply nested function.
Although suppressed within this documentation for clarity, all CoffeeScript output (except in files with or statements) is wrapped in an anonymous function: . This safety wrapper, combined with the automatic generation of the keyword, make it exceedingly difficult to pollute the global namespace by accident. (The safety wrapper can be disabled with the option, and is unnecessary and automatically disabled when using modules.)
If you’d like to create top-level variables for other scripts to use, attach them as properties on ; attach them as properties on the object in CommonJS; or use an statement. If you’re targeting both CommonJS and the browser, the existential operator (covered below), gives you a reliable way to figure out where to add them: .
Since CoffeeScript takes care of all variable declaration, it is not possible to declare variables with ES2015’s or . This is intentional; we feel that the simplicity gained by not having to think about variable declaration outweighs the benefit of having three separate ways to declare variables.
If, Else, Unless, and Conditional Assignment
/ statements can be written without the use of parentheses and curly brackets. As with functions and other block expressions, multi-line conditionals are delimited by indentation. There’s also a handy postfix form, with the or at the end.
Splats, or Rest Parameters/Spread Syntax
Splats also let us elide array elements…
…and object properties.
In ECMAScript this is called spread syntax, and has been supported for arrays since ES2015 but is coming soon for objects. Until object spread syntax is officially supported, the CoffeeScript compiler outputs the same polyfill as Babel’s rest spread transform; but once it is supported, we will revise the compiler’s output. Note that there are very subtle differences between the polyfill and the current proposal.
Loops and Comprehensions
Most of the loops you’ll write in CoffeeScript will be comprehensions over arrays, objects, and ranges. Comprehensions replace (and compile into) loops, with optional guard clauses and the value of the current array index. Unlike for loops, array comprehensions are expressions, and can be returned and assigned.
Which is almost certainly not what you want.