2012年3月30日金曜日

Node.js をnvmで管理、foreverインストールで問題?

最近Node.js で遊んでますが。v0.4.11 -> v0.6.14 -> v0.7.6 の3つをnvmでインストールしてみてます。某vpsなんだけどね。forever入れようとして全然ダメでした。これが何に起因する問題なのかわからないですけど、一応解決できたのでメモとしてエントリ。2012年3月末の時点でのログです。 nvmを使わずにnode.jsをインストールしちゃった人は、 /usr/local/bin /usr/local/lib /usr/local/include あたりからnodeのディレクトリを全部削除して 入れたらいい。
rm -rf /usr/local/bin/node
rm -rf /usr/local/lib/node
rm -rf /usr/local/include/node
rm -rf /usr/local/share/man/man1/node.1
rm -rf /usr/local/lib/node_modules
nvmはgithubから
git clone git://github.com/creationix/nvm.git .nvm
でもってきて、nvm.shをカレントのshellで実行しておく
source /somewhere/.nvm/nvm.sh
で、nodeのバージョン指定してnode.jsをインストール。
nvm install v0.7.6
これで普通にnode.js入れられる。nvmはnpmも一緒に入れてくれるのでnaveよりいいと思う。 バージョンを切り替えるのも簡単で、インストールさえしておけば
nvm use v0.4.11
とかでOK。ただ、環境変数とかちゃんと設定してから使いましょう。それから、各ユーザの設定で
if [ -s /root/.nvm/nvm.sh ]; then
    . /root/.nvm/nvm.sh
    export NODE_PATH=${NVM_PATH}_modules
fi
こんなのをbashrcに書いておくと楽。あと、
nvm alias default 0.7
とかもやっといたほうがいいかな? で、問題のforerverインストールですが
npm install forerver -g
だとエラーエラーで止まります。
npm ERR! Error: No compatible version found: node-fork@'>=0.4.0- <0.5.0-'
npm ERR! No valid targets found.
npm ERR! Perhaps not compatible with your version of node?
npm ERR!     at installTargetsError (/root/.nvm/v0.7.6/lib/node_modules/npm/lib/cache.js:488:10)
npm ERR!     at next_ (/root/.nvm/v0.7.6/lib/node_modules/npm/lib/cache.js:438:17)
npm ERR!     at next (/root/.nvm/v0.7.6/lib/node_modules/npm/lib/cache.js:415:44)
npm ERR!     at /root/.nvm/v0.7.6/lib/node_modules/npm/lib/cache.js:408:5
npm ERR!     at saved (/root/.nvm/v0.7.6/lib/node_modules/npm/lib/utils/npm-registry-client/get.js:147:7)
npm ERR!     at Object.oncomplete (/root/.nvm/v0.7.6/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:231:7)
npm ERR! You may report this log at:
npm ERR!     
npm ERR! or email it to:
npm ERR!     
npm ERR!
npm ERR! System Linux 2.6.18-164.10.1.el5.028stab067.4
npm ERR! command "/root/.nvm/v0.7.6/bin/node" "/root/.nvm/v0.7.6/bin/npm" "install" "forever" "-g"
npm ERR! cwd /root
npm ERR! node -v v0.7.6
npm ERR! npm -v 1.1.8
npm ERR! message No compatible version found: node-fork@'>=0.4.0- <0.5.0-'
npm ERR! message No valid targets found.
npm ERR! message Perhaps not compatible with your version of node?
npm http 304 https://registry.npmjs.org/pkginfo
npm http 304 https://registry.npmjs.org/minimatch
npm http 304 https://registry.npmjs.org/portfinder
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /root/npm-debug.log
npm not ok
適当にGoogle先生に聞いてみると、いつぞやのIRCのログで -f つければいいよ、てのを見つけたので早速
npm install forever -g -f
これで全部入ることは入る。でも、なぜかコンパイルエラーが出ます。自分の環境だと
microtime
daemon
node-fork
はエラーなので、これを手で修正、といっても2度ビルドするだけです。 意味不明だけど2度ビルドするとエラー消える。
cd nvm/v0.7.6/lib/node_modules/forever/node_modules
cd microtime
node-waf configure build
node-waf configure build

cd ../
cd daemon
sh ./install
sh ./install

cd ../
cd node-fork
sh ./install
sh ./install
ここまできたらforerverコマンドを叩いてみる
[root@localhost ~]# forever
path.existsSync is deprecated. It is now called `fs.existsSync`.
help:   usage: forever [action] [options] SCRIPT [script-options]
help:
help:   Monitors the script specified in the current process or as a daemon
help:
help:   actions:
help:     start               Start SCRIPT as a daemon
help:     stop                Stop the daemon SCRIPT
help:     stopall             Stop all running forever scripts
help:     restart             Restart the daemon SCRIPT
help:     restartall          Restart all running forever scripts
help:     list                List all running forever scripts
help:     config              Lists all forever user configuration
help:     set       Sets the specified forever config 
help:     clear          Clears the specified forever config 
help:     logs                Lists log files for all forever processes
help:     logs  Tails the logs for 
help:     columns add    Adds the specified column to the output in `forever list`
help:     columns rm     Removed the specified column from the output in `forever list`
help:     columns set   Set all columns for the output in `forever list`
help:     cleanlogs           [CAREFUL] Deletes all historical forever log files
help:
help:   options:
help:     -m  MAX          Only run the specified script MAX times
help:     -l  LOGFILE      Logs the forever output to LOGFILE
help:     -o  OUTFILE      Logs stdout from child script to OUTFILE
help:     -e  ERRFILE      Logs stderr from child script to ERRFILE
help:     -p  PATH         Base path for all forever related files?(pid files, etc.)
help:     -c  COMMAND      COMMAND to execute (defaults to node)
help:     -a, --append     Append logs
help:     --pidfile        The pid file
help:     --sourceDir      The source directory for which SCRIPT is relative to
help:     --minUptime      Minimum uptime (millis) for a script to not be considered "spinning"
help:     --spinSleepTime  Time to wait (millis) between launches of a spinning script.
help:     --plain          Disable command line colors
help:     -d, --debug      Forces forever to log debug output
help:     -v, --verbose    Turns on the verbose messages from Forever
help:     -s, --silent     Run the child script silencing stdout and stderr
help:     -w, --watch      Watch for file changes
help:     -h, --help       You're staring at it
help:
help:   [Long Running Process]
help:     The forever process will continue to run outputting log messages to the console.
help:     ex. forever -o out.log -e err.log my-script.js
help:
help:   [Daemon]
help:     The forever process will run as a daemon which will make the target process start
help:     in the background. This is extremely useful for remote starting simple node.js scripts
help:     without using nohup. It is recommended to run start with -o -l, & -e.
help:     ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
help:         forever stop my-daemon.js
help:
なんかいけたっぽいですね。deprecatedな警告が出てたりしますが、動作確認はこれからですけど、多分大丈夫でしょう。C++のコードとかでビルドエラーになるって人は試してみたらいいと思います。
#メモリが足らないのかなぁ。

 以上。log4jsも同様にやっつけましたw。某vpsの一番安いのでも全然遊べるって話。