node.jsの環境設定

前にnode.jsの記録を書いたけど、
node.js で遊ぶ
環境について、改めてまとめておきます。
■前提
OS: debian 9 (stretch)
$ uname -a
Linux tori 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64 GNU/Linux

■node.js と npmのインストール
参考:How to Install Latest Nodejs & NPM on Debian 9/8/7

How to Install Node.js on Debian 12/11/10


ここでは最新版ではなく、安定版(LTS Release)をインストールする。
$ sudo apt-get install curl software-properties-common
$ curl -sL https://deb.nodesource.com/setup_8.x | sudo bash –
$ sudo apt-get install -y nodejs
バージョンを確認する。
$ nodejs -v
v8.11.2
$ npm -v
5.6.0
■準備
ホームディレクトリ直下にnodeフォルダを作成する。
$ cd ~/
$ mkdir node
$ cd node
$ npm init
■expressをインストール
$ cd ~/node
$ npm install express
■テスト、次で動作確認。
$ nodejs example.js
次のエラーが出る場合、すでにプロセスが走っているかも
$ events.js:183
throw er; // Unhandled ‘error’ event
^
参考:Node.jsの throw er; // Unhandled ‘error’ eventエラー@万年素人からGeekへの道
http://d.hatena.ne.jp/shinriyo/20140105/p3
■forever install
参考:verjs/forever @GitHub
https://github.com/foreverjs/forever
$ cd ~/node
$ npm install forever
使い方は次の通り。
$ forever start example.js
$ forever list
$ forever stop example.js