Last Updated: February 25, 2016
·
1.953K
· Rilke Petrosky (XenoMuta)

Painless coffeescript traces and debugging with coffee-trace

Coffee-Trace
Coffee-Trace makes spotting javascript stack traces back to coffee-script source code a friendly tasks, and with such style that you might start enjoying when things crashes ( not really ).

Simply install coffee-trace:
npm install coffee-trace

and then require() it before all your other libraries
require("coffee-trace")

or straight from command line:
coffee -rcoffee-trace my-program.coffee

Example
Running this..

require('coffee-trace')

test = ->
  people =
    john:
      first_name: 'john'
      last_name: 'doe'
    mary:
      first_name: 'mary'
      last_name: 'jane'

  console.log("Welcome", people[p].first_name, people[p].last_name, "!!!") for p in ['john', 'mary', 'josh']

process.nextTick test

will result in this:
Coffee-Trace

3 Responses
Add your response

This sounds useful. However since it needs to be the very first require, it can't be a conditional inside my code after already checking for the env value, which I use nconf for. And then, -rcoffee-trace isn't recognized by coffeescript (using coffeescript 1.4.0). I think you meant coffee -r ./node_modules/coffee-trace in a typical installation, and certainly not -rcoffeescript as one word. But even when using the later to run my app, it doesn't work.

Works for the sample you provide, but not for other exceptions. They show very uncaffeinated. E.g. I get stuff like:

Error: listen EACCES
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1020:19)
    at listen (net.js:1061:10)
    at Server.listen (net.js:1127:5)
    at Object.<anonymous> (/home/matan/ingi/repos/front-end/app.coffee:100:10)
    at Object.<anonymous> (/home/matan/ingi/repos/front-end/app.coffee:112:4)
    at Module._compile (module.js:456:26)
    at Object.exports.run (/usr/lib/coffee-script/lib/coffee-script/coffee-script.js:83:25)
    at compileScript (/usr/lib/coffee-script/lib/coffee-script/command.js:177:29)
    at /usr/lib/coffee-script/lib/coffee-script/command.js:152:18

Maybe it's related to coffee-trace's async initialization and instrumentation?

over 1 year ago ·

Its exactly what you suspect. It is initialized in an async fashion. You could write a wrapper for your program which requires coffee-trace and then, in the next loop carries on with your app with require ( with nextTick() maybe ).
Radical changes in the way coffee-trace works are planned for a rewrite, however, it may no longer be as necessary as before, because there has being a lot of progress with sourcemaps and later versions of coffee-script match lines correctly most of times.
Always use the latest version of coffee-script and coffee-trace as well.

over 1 year ago ·

Mmmm thanks, much appreciated :)
So then the advice for using -r should be taken with a grain of salt shouldn't it? I didn't notice I was working with an old version until I checked now. WIth the latest CoffeeScript 1.6.3 stack traces are a bit more verbose, but are still clean of any CoffeeScript line references.... only pure javascript ones. So I'm not entirely sure what you meant in 'may no longer be as necessary as before` there......

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip