Wong Liang Zan

Wong Liang Zan

© 2020

How to use Coffeescript in your Node.js project

Coffeescript is a popular language that compiles to Javascript. Many developers prefer to write in Coffeescript due to its elegant syntax. If you want to use Coffeescript to write your Node.js project, you can do so easily. I’m going to write a small project as an example of how to use Coffeescript in your project.

Querying for the weather

We will make use of Node.js to query for the weather information using Google’s unofficial weather API. It will be a simple command line script.

Installing Coffeescript

There are a variety of ways to install Coffeescript. Let’s make use of package.json to install our packages. You could add Coffeescript to your list of dependencies in package.json

"dependencies": {
  "coffee-script": "~1.3.3",
}

And run

npm install

to install it. If not you can install it directly with npm.

npm install coffee-script

You can also install it via your package manager. Most of them should provide a Coffeescript package.

Compiling Coffeescript

When you are done writing your code, you have to compile the Coffeescript file to Javascript. Compiling is as simple as running the following command

coffee -c foo.coffee

That will output a foo.js.

Once you have a lot of Coffeescript files, compiling by hand becomes tedious. Currently many open source Node.js projects prefer to use GNU Make. You can make use of make to compile your Coffeescript files into Javacript. Below is an example.

compile:
	@find src -name '*.coffee' | xargs ./node_modules/.bin/coffee -c -o bin

Save the script as Makefile. This compile taget assumes three things. One, your Coffeescript files are under the src directory. Two, your coffee executable is found under node_modules. Three, you want your compiled Javscript files to be saved under the bin directory. Do replace if otherwise. Currently the convention is to put the Coffeescript files under the src directory. And then compile to the lib or bin directory. You can compile all your Coffeescript files by running the command below.

make compile

The compiled files can be found under the bin directory.

Automating the compilation

As you develop the code, you will find the process of compilation tedious. There are ways to automate the compilation.

Guard

Guard is a Ruby gem that can run scripts on file changes.

Below is a sample Guardfile that watches Coffeescript files for changes and compiles them.

guard :compile-coffeescript do
  watch(%r{^src/*.coffee$}) { `make compile` }
end

Compile on run

Another way is to compile before running the script. We can add a run target in the Makefile with a compile dependency. You can add the below to your Makefile to compile to Javascript before running the script.

run: compile
	node ./bin/index.js singapore

You can run the following to see the results.

make run

The only caveat is you cannot pass arguments to the script elegantly. There is a workaround though.

Conclusion

Using Coffeescript to develop your Node.js project is simple. Simply setup your project to compile your source and that is it.