官术网_书友最值得收藏!

Debugging CoffeeScript using Node Inspector

Source maps and Chrome's developer tools can help troubleshoot our CoffeeScript that is destined for the Web. In this recipe, we will demonstrate how to debug CoffeeScript that is designed to run on the server.

Getting ready

Begin by installing the Node Inspector NPM module with the following command:

npm install -g node-inspector

How to do it...

To use Node Inspector, we will use the coffee command to compile the CoffeeScript code we wish to debug and generate the source map.

In our example, we will use the following simple source code in a file named counting.coffee:

for i in [1..10]
  if i % 2 is 0
    console.log "#{i} is even!"
  else
    console.log "#{i} is odd!"

To use Node Inspector, we will compile our file and use the source map parameter with the following command:

coffee -c -m counting.coffee

Next, we will launch Node Inspector with the following command:

node-debug counting.js

How it works...

When we run Node Inspector, it does two things.

First, it launches the Node debugger. This is a debugging service that allows us to step through code, hit line breaks, and evaluate variables. This is a built-in service that comes with Node. Second, it launches an HTTP handler and opens a browser that allows us to use Chrome's built-in debugging tools to use break points, step over and into code, and evaluate variables.

Node Inspector works well using source maps. This allows us to see our native CoffeeScript code and is an effective tool to debug server-side code.

The following screenshot displays our Chrome window with an active break point. In the local variables tool window on the right-hand side, you can see that the current value of i is 2:

How it works...

The highlighted line in the preceding screenshot depicts the log message.

主站蜘蛛池模板: 高清| 嘉祥县| 金沙县| 故城县| 宜兰市| 乌拉特中旗| 武平县| 洪泽县| 延寿县| 寻甸| 泗水县| 三门峡市| 苍南县| 探索| 东源县| 灌云县| 通州区| 西平县| 同仁县| 合川市| 巨野县| 宁蒗| 赤壁市| 迭部县| 涟水县| 巨鹿县| 浮梁县| 宜昌市| 澄江县| 崇明县| 商南县| 新余市| 新乡县| 漯河市| 清水县| 深泽县| 西藏| 长宁县| 阿坝县| 玛纳斯县| 柘城县|