Coffee Script Command-line utility - CoffeeScript

What is Coffee Script Command-line utility?

On installing Coffee Script on Node.js, we can get entry to the coffee-command line utility. In here, the coffee command is the important command. Using various options of this command, we are able to bring together and execute the Coffee Script documents.
You may see the list of alternatives of the coffee command using its -h or --assist choice. Open the Node.js command prompt and execute the following command in it.

This command gives you the list of various options of the coffee, along with the description of the operation performed by each of them as shown below.

coffeecommand line

Compiling the Coffee Script Code

The Coffee Script files are saved with the extension .coffee. You can compile these files using the -c or --compile option of the coffee command as shown below.


Suppose there is a file in your system with the following CoffeeScript code which prints a message on the console.

note − The console.log() function prints the given string at the console.

To assemble the above code, save it in a record with the name sample. Coffee. Open the Node.js command activates. Browse through the route wherein you have got saved the document and bring together it the usage of the -c choice of the coffee command of the coffee command-line application as proven under.

On executing the above command, the CoffeeScript compiler compiles the given file ( and saves it in the current location with a name sample.js as shown below.


If you open the sample.js file, you can observe the generated JavaScript as shown below.

Executing the CoffeeScript code

You can execute a CoffeeScript file by simply passing the file name to the coffee command in the Node.js command prompt as follows.


As an instance, let us execute the sample. Coffee document. For this, open the Node.js command prompt. Browse through the direction wherein you have got stored the file and execute the file by immediately passing its name to the coffee command as shown underneath.


Watch and compile

In a few scenarios, there may be a chance that we do a variety of adjustments to our scripts. the usage of the –w option of the coffee command, you watch your scripts for adjustments.

You may watch and collect a report simultaneously using the -WC choice as shown under. while we use this option, the record will be recompiled every time you make changes in your script.


Assume we've compiled a record named the use of the -WC option and we modified the script three times. On every occasion we exchange the script, the .coffee file is recompiled leaving the Node.js command prompt as shown below.


Setting the Output Directory

Using the -o option, we can set the output directory to place the compiled JavaScript files as shown below.


Let us save the JavaScript code of the file in a folder named data in the E drive using the -o option by executing the following command in the command prompt.

Following is the snapshot of the given folder after executing the above command. Here you can observe the JavaScript file of the


Print the Compiled JavaScript

If we want to print the compiled javascript on the console itself, we have to use the -p option of the coffee command as shown below.


For example, you can print the compiled JavaScript code of the file on the console using the -p option as shown below.


The REPL (read evaluate Print Loop)

Coffee Script offers you a REPL-interactive shell. This shell is used to evaluate the Coffee Script expressions. You may type any Coffee Script code in this shell and get the result without delay. You can open REPL by using executing the coffee command with none options as shown underneath.


Using this shell, we are able to assign values to variables, create functions, and evaluate outcomes. As shown in the following screenshot, if we call capabilities in REPL, it prints the price of the function. If we give an expression to it, it evaluates and prints the end result of the expression. And if we in reality kind the statements in it, it prints the cost of the closing declaration.


In REPL, you could access more than one line mode with the aid of pressing ctrl+v wherein you can evaluate the code with a couple of traces (like capabilities) and you can get back to REPL mode from it by way of pressing ctrl+v again. Here is an example usage of the multi-line mode.

multiline function

Running CoffeeScript through Browser

We can run CoffeeScript using the <script> tag of the HTML just like JavaScript as shown below.

However, for this, we should import the library in every application and the CoffeeScript code might be interpreted line by line earlier than the output is shown. this could gradual down your packages, therefore this method isn't always recommended.

Therefore, to use CoffeeScript in your applications, you need to pre-compile them using the coffee command-line application and then you may use the generated JavaScript in your packages.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

CoffeeScript Topics