LoginSignup
8
8

More than 5 years have passed since last update.

Node.js の デバッグ方法と live-edit でのファイル保存とバージョン管理、自分めも、その3

Posted at

デバッグ方法

なんだかプログラムするより環境整備に目が行ってしまうわたし、そして横道それまくりなのです。が、本にも掲載されている Node.js の デバッグ方法 ですが気になった点があったので、自分メモがてらに書いておきます。

Node.jsの開発時に必要な情報

node-inspector によるデバッグ

Node.js の 一般的デバッグ方法は、node-inspector で行うらしいのでまずインスコします(環境によって sudo つける必要がありますw)。

$ npm install -g node-inspector

公式サイトみるとサンプルの config file イメージがあるので同じようにファイルを作成します(場所は、用途によって色々な場所に設置できるみたい)。

web-host は、null 指定だと 127.0.0.1 になりますが、いつものようにVagrant環境で動かしているので ip を指定します。

~/.node-inspectorrc
{
  "web-port": 8088,
  "web-host": "192.168.11.212",
  "debug-port": 5858,
  "save-live-edit": true,
  "preload": false,
  "nodejs": ["--harmony"]
}

ターミナルを2つ開いて

$ node --debug sample_api4.js
debugger listening on port 5858
$ node-inspector 
Node Inspector v0.9.2
Visit http://192.168.11.212:8088/debug?port=5858 to start debugging.

と表示されれば、ok。

Chromeによるデバッグ

node-inspector は、Chrome のDeveloperモードを経由してデバッグを行うので、IE や FireFox ではデバッグできないよw。

image

live-edit 上の変更と保存

"save-live-edit": true で定義している場合は、編集モードで変更・追加した内容が直接ファイルに保存されます。false を指定した場合には、Warnig コンソールに以下の内容が表示されます。利便性を考えると true だけど、安全性を求めるなら false なのかな?

Saving of live-edit changes back to source files is disabled by configuration.
Change the option "saveLiveEdit" in config.json to enable this feature.

バージョンが古いって言われたw

yum で Node.js をインストールすると v0.10.33(2015/04/22現在) なので最新版とくらべるとちょっと古い。なので nvm を導入します。
※ついでに node-inspector からも Update Node to 0.11.13 or newer to get full support. と表示されてるしね。

$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ source ~/.nvm/nvm.sh

インストールされている Node.js のバージョンとインストール可能なバージョンの確認

$ nvm ls
$ nvm ls-remote

最新の Node.js をインストールして切り替えてみる。

$ nvm install 0.12.2
######################################################################## 100.0%
Now using node v0.12.2 (npm v2.7.4)
$ nvm use 0.12.2
Now using node v0.12.2 (npm v2.7.4)
$ node -v
v0.12.2

.bashrc に、source ~/.nvm/nvm.sh を追加します。

あと use コマンドで利用しているバージョンを指定しても、再起動の度にインストールされている中で最新のバージョンがデフォルトで利用される?ので、そのデフォルト設定をしておきます。
system の文字列は、yum でいれた版になります。

$ nvm alias default 0.12.2
default -> 0.12.2 (-> v0.12.2)
$ nvm alias default system
default -> system

最新にして、node-inspector を再起動。。。これで下記のメッセージがでなくなりましたよねw(キャッシュが残ってるときもあるのでF5ねw)

Your Node version (undefined) has a partial support of profiler.
The stack frames tree doesn't show all stack frames due to low sampling rate.
The profiling data is incomplete and may show misleading results.
Update Node to 0.11.13 or newer to get full support.
8
8
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
8
8