LoginSignup
19
10

More than 5 years have passed since last update.

Node.jsでRubyのbinding.pry的ツール、debuggerを使う

Posted at

Node.jsでbinding.pryを使いたい。
Debuggerというツールが組み込まれているようだ。
Node.js v7.7.2 Documentation Debugger

配列を作ってループをブレイクしてみる。

debugtest.js
Array.from(Array(3).keys()).forEach(function(key){
 debugger; // ここでブレイクする
 console.log(key)
})

debugオプションを付けて実行。

node debug debugger.js

< Debugger listening on 127.0.0.1:5858
debug>  ok
break in test.js:1
> 1 Array.from(Array(3).keys()).forEach(function(key){
  2   debugger;
  3   console.log(key)

contコマンドでブレイクポイントまで進む。

debug> cont
break in test.js:2
  1 Array.from(Array(3).keys()).forEach(function(key){
> 2   debugger;
  3   console.log(key)
  4  })

replコマンドで対話モードに入る。Ctrl + Cで出る。

debug> repl
Press Ctrl + C to leave debug repl
> key
0
debug> cont
< 0
break in test.js:2
  1 Array.from(Array(3).keys()).forEach(function(key){
> 2   debugger;
  3    console.log(key)
  4  })
debug> repl
Press Ctrl + C to leave debug repl
> key
1

使えた。

テストでも使いたい。mochaの場合はこれで同じようにいけた。便利。

mocha debug <file-name>
19
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
10