CoffeeScript - Command-line utility

On installing CoffeeScript on Node.js, we can access the coffee-command line utility. In here, the coffee command is the key command. Using various options of this command, we can compile and execute the CoffeeScript files.
You can see the list of options of the coffee command using its -h or --help option. Open the Node.js command prompt and execute the following command in it.
c:\>coffee -help

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.
The CoffeeScript 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.
c:\>coffee -c filename.coffee

Suppose there is a file in your system with the following CoffeeScript code which prints a message on the console.
name = "Raju"
console.log "Hello"+name+" Welcome to Tutorialspoint"
Note − The console.log() function prints the given string on the console.
To compile the above code, save it in a file with the name sample.coffee. Open the Node.js command prompt. Browse through the path where you have saved the file and compile it using the -c option of the coffee command of the coffee command-line utility as shown below.
c:\> coffee -c sample.coffee

On executing the above command, the CoffeeScript compiler compiles the given file (sample.coffee) 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.
// Generated by CoffeeScript 1.10.0
(function() {
  var name;
  name = "Raju";
  console.log("Hello " + name + " Welcome to Tutorialspoint");

}).call(this);
You can execute a CoffeeScript file by simply passing the file name to the coffee command in the Node.js command prompt as follows.
c:\> coffee sample.coffee

For example, let us execute the sample.coffee file. For this, open the Node.js command prompt. Browse through the path where you have saved the file and execute the file by directly passing its name to the coffee command as shown below.
In some scenarios, there is a chance that we do a lot of changes to our scripts. Using the –w option of the coffee command, you watch your scripts for changes.
You can watch and compile a file simultaneously using the -wc option as shown below. When we use this option, the file will be recompiled each time you make changes in your script.
c:\>coffee -wc file_name

Suppose we have compiled a file named sample.coffee using the -wc option and we modified the script thrice. Each time we change the script, the .coffee file is recompiled leaving the Node.js command prompt as shown below.
Using the -o option, we can set the output directory to place the compiled JavaScript files as shown below.
c:\>coffee -o "Required path where we want our .js files" file_name

Let us save the JavaScript code of the sample.coffee file in a folder named data in the E drive using the -o option by executing the following command in the command prompt.
c:\>coffee -o E://data sample.coffee

Following is the snapshot of the given folder after executing the above command. Here you can observe the JavaScript file of the sample.coffee
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.
c:\>coffee -p file_name

For example, you can print the compiled JavaScript code of the sample.coffee file on the console using the -p option as shown below.
CoffeeScript provides you an REPL-interactive shell. This shell is used to evaluate the CoffeeScript expressions. You can type any CoffeeScript code in this shell and get the result immediately. You can open REPL by executing the coffee command without any options as shown below.
Using this shell, we can assign values to variables, create functions, and evaluate results. As shown in the following screenshot, if we call functions in REPL, it prints the value of the function. If we give an expression to it, it evaluates and prints the result of the expression. And if we simply type the statements in it, it prints the value of the last statement.
In REPL, you can access multiple line mode by pressing ctrl+v where you can evaluate the code with multiple lines (like functions) and you can get back to REPL mode from it by pressing ctrl+v again. Here is an example usage of the multi line mode.
We can run CoffeeScript using the <script> tag of the HTML just like JavaScript as shown below.
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
   type="text/javascript" charset="utf-8"></script>
<script type="text/coffeescript">
  # Some CoffeeScript
</script>
But for this, we have to import the library in each application and the CoffeeScript code will be interpreted line by line before the output is shown. This will slow down your applications, therefore this approach is not recommended.
Therefore, to use CoffeeScript in your applications, you need to pre-compile them using the Coffee command-line utility and then you can use the generated JavaScript in your applications.
 Print
 Add Notes
 Bookmark this page