nodejs debugging with non-javascript

Viewed 106

In my case, I want to debug CoffeeScript, but this question is NOT CoffeeScript specific. It relates to debugging ANY non-JavaScript language that compiles to JavaScript. To do that, you need to create a sourcemap, which nodejs is capable of. But then, I want to see and step through and inspect variables in my CoffeeScript code, knowing that nodejs will actually be executing the corresponding JavaScript code - mapping the line numbers between them by using the sourcemap. Someone will, no doubt, ask for my source code, so let's just use this (call it temp.coffee):

orders = [341, 454, 198, 264, 307]
totalOrders = 0
debugger
for order in orders
    totalOrders += order
console.log totalOrders

Note that I added a 'debugger' statement, though that should not be necessary - by default, nodejs should stop just before executing the first statement. I've actually come close to achieving my goal by doing this in my terminal:

coffee -cm temp.coffee
node inspect temp.coffee

The first statement compiles temp.coffee into temp.js, also producing file temp.js.map - a sourcemap file. The second statement also appears to work - it tells me that the debugger is attached and gives me a prompt. Unfortunately, if I try to execute the 's' command to step over the next statement in my program, it tells me "Uncaught Error [ERR_DEBUGGER_ERROR]: Can only perform operation while paused." Why is it not paused??? I've tried using the 'run' or 'restart' commands, but I get the same result. I'm hoping that I'm just forgetting to do something and that someone can clue me in.

1 Answers

Your coffee file isn't being loaded as a node script probably because its not a valid file extension. You can observe this by using the scripts command

$ node inspect temp.coffee
< Debugger listening on ws://127.0.0.1:9229/b8f44e67-51c6-428b-8010-ccc02a30459a
< For help, see: https://nodejs.org/en/docs/inspector
<
connecting to 127.0.0.1:9229 ... ok
< Debugger attached.
<
< Waiting for the debugger to disconnect...
<
debug> scripts
  23: node:events
  24: node:buffer
  35: node:async_hooks
  37: node:stream
  41: node:util
  43: node:timers
  55: node:string_decoder
  58: node:stream/promises
  63: node:path
  65: node:fs
  68: node:querystring
  73: node:v8
  76: node:vm
  77: node:url
  80: node:crypto
  138: node:net
debug>

Notice the lack of any temp name in any scripts. The file your debugging should be a script loaded.


Running node inspect temp.js is debugging using nodes CLI debugger. This debugger is a lightweight CLI variant of a debugger but doesn't necessarily have all the fancy features of modern debuggers you might expect (such as source map support). The node team seems to be in favor of letting remote debuggers do the work for those features: https://github.com/nodejs/node/issues/6725

You can see your source maps at work by using a different debugger, the Getting Started Debugging guide from Node is a good place to figure out how to start.

For example

$ node --inspect-brk=127.0.0.1:9200 temp.js

Allows me to debug it in Chrome DevTools and be debugging the coffeescript itself.


Not all source maps have this but you can see the entire contents of your coffeescript file in your .sourcemap file. In fact you could delete your temp.coffee file and debug just the js file and it should work just fine

{
  "version": 3,
  "file": "temp.js",
  "sourceRoot": "\\",
  "sources": [
    "temp.coffee"
  ],
  "names": [],
  "mappings": ";AAAA;AAAA,MAAA,CAAA,EAAA,GAAA,EAAA,KAAA,EAAA,MAAA,EAAA;;EAAA,MAAA,GAAS,CAAC,GAAD,EAAM,GAAN,EAAW,GAAX,EAAgB,GAAhB,EAAqB,GAArB;;EAET,WAAA,GAAc;;AACd;;EACA,KAAA,wCAAA;;IACE,WAAA,IAAe;EADjB;;EAEA,OAAO,CAAC,GAAR,CAAY,WAAZ;AANA",
  "sourcesContent": [
    "orders = [341, 454, 198, 264, 307]\r\n\r\ntotalOrders = 0\r\ndebugger\r\nfor order in orders\r\n  totalOrders += order\r\nconsole.log totalOrders\r\n"
  ]
}

Chrome Devtools

Related