53

Is there a way to debug CoffeeScript line-by-line?

I understand that it compiles into Javascript. But this sounds like it could make it a pain to debug.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
  • 4
    Since the generated JavaScript preserves indentation, names of functions and variables debugging it's not nearly as bad as most people think. Also, support for "Source Maps" is coming (in the next version?) in CoffeeScript to address this: http://www.coffeescriptlove.com/2012/04/source-maps-for-coffeescript.html – Hector Correa Jun 19 '12 at 14:13
  • 1
    You can configure [coffeescript debbuging in JetBrains WebStorm](http://www.youtube.com/watch?feature=player_detailpage&v=Sl1Uk3zT5Fg) – Maxim Yefremov Apr 02 '13 at 16:09

6 Answers6

36

Update: there's currently a redesign of coffeescript compiler that generates source maps, which should enable you to debug your coffeescript in most recent versions of Google Chrome (18 and upwards I think).

I think it's not production-ready yet, but it's worth mentioning.

Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
  • 1
    Pay attention that source maps are available only for Chrome Canary, WebKit nightly and Firefox 23+. BTW, that's will be the best way to debug compiled javascript code at client-side, so +1 for ur answer ;) – Wilk Jul 02 '13 at 13:22
  • Available in Chromium 39, current version in Ubuntu Trusty repositories. And, well, "non-production-readiness" doesn't restrict its usage during development. – D-side Feb 04 '15 at 10:09
29

if you are running coffeescript from the terminal you can debug it line-for-line using node-inspector, launching your script this way:

coffee --nodejs --debug-brk yourscript.coffee
Lloyd
  • 8,204
  • 2
  • 38
  • 53
14

At the moment it is quite a pain to debug CoffeeScript. Most people use lots of unit tests.

There is some work being done on debugging for CoffeeScript but it is probably a while away before we'll have a really good debugger. One example is http://www.infoq.com/news/2011/08/debug-languages-on-javascript-vm

leonm
  • 6,454
  • 30
  • 38
  • 16
    Creepy unit tests to avoid happy call-stack debugging?? Yikes! – Esteban Jun 17 '12 at 18:38
  • I'll drop bugger here: https://github.com/jkrems/bugger. It has step-by-step debugging for coffee-script 2.x. For coffee-script 1.x there's the strongloop-fork of node-inspector: https://github.com/strongloop/node-inspector – Jan Olaf Krems Jul 22 '13 at 11:51
9

Yes, with node-inspector:

npm install -g node-inspector

By putting the statement debugger into the source code of your script, you will enable a breakpoint. Then type in a console:

coffee -c -m myscript.coffee
node-debug myscript.js

Node Inspector supports source-maps out of the box, so no extra configuration is needed.

For more information see this post.

srus
  • 319
  • 2
  • 8
5

Coffeescript now supports source maps: http://coffeescript.org/

Jetbrains for example supports this feature: https://blog.jetbrains.com/ruby/2013/01/whats-mining-coffeescript-debugger/

lhk
  • 27,458
  • 30
  • 122
  • 201
1

now is 2020, i find this question, and then i find vscode support sourcemap, so we can use vscode to debug coffee directly.

btw, i think coffee need a great improve. just like static data. anyway here is my launch.json:

{
    "version": "0.2.0",
    "configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${file}", //important, make sure debug current file
        "outFiles": [
            "${workspaceFolder}/dist/api/api.js" //important, where to find sourcemap js file
        ]
    }]
}
defend orca
  • 617
  • 7
  • 17