console(Consoleクラスのグローバルインスタンス)

今回は、Node.js。

Node.js
Japan Node.js Association

Node.jsをセットアップする。

Node.jsをFedora26にインストールして動作テスト | atominux

$ sudo dnf install nodejs npm
[sudo] bg1 のパスワード:
メタデータの期限切れの最終確認: xx:xx:xx 時間前の xxxx年xx月xx日 xx時xx分xx秒 に実施しました。
依存関係が解決しました。
================================================================================
 パッケージ       アーキテクチャー
                              バージョン                     リポジトリ   サイズ
================================================================================
インストール:
 nodejs           x86_64      1:10.15.0-1.fc29               updates      6.3 M
 npm              x86_64      1:6.4.1-1.10.15.0.1.fc29       updates      3.6 M
依存関係をインストール中:
 http-parser      x86_64      2.9.0-1.fc29                   updates       35 k
 libuv            x86_64      1:1.26.0-1.fc29                updates      126 k

トランザクションの概要
================================================================================
インストール  4 パッケージ

ダウンロードサイズの合計: 10 M
インストール済みのサイズ: 45 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード中です:
(1/4): libuv-1.26.0-1.fc29.x86_64.rpm            38 kB/s | 126 kB     00:03
(2/4): http-parser-2.9.0-1.fc29.x86_64.rpm      9.0 kB/s |  35 kB     00:03
(3/4): nodejs-10.15.0-1.fc29.x86_64.rpm         440 kB/s | 6.3 MB     00:14
(4/4): npm-6.4.1-1.10.15.0.1.fc29.x86_64.rpm    322 kB/s | 3.6 MB     00:11
--------------------------------------------------------------------------------
合計                                            601 kB/s |  10 MB     00:17
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  scriptletの実行中: npm-1:6.4.1-1.10.15.0.1.fc29.x86_64                    1/1
  準備             :                                                        1/1
Installed: libuv-1:1.26.0-1.fc29.x86_64
  インストール中   : libuv-1:1.26.0-1.fc29.x86_64                           1/4
Installed: libuv-1:1.26.0-1.fc29.x86_64
Installed: http-parser-2.9.0-1.fc29.x86_64
  インストール中   : http-parser-2.9.0-1.fc29.x86_64                        2/4
Installed: http-parser-2.9.0-1.fc29.x86_64
Installed: npm-1:6.4.1-1.10.15.0.1.fc29.x86_64
  インストール中   : npm-1:6.4.1-1.10.15.0.1.fc29.x86_64                    3/4
Installed: npm-1:6.4.1-1.10.15.0.1.fc29.x86_64
Installed: nodejs-1:10.15.0-1.fc29.x86_64
  インストール中   : nodejs-1:10.15.0-1.fc29.x86_64                         4/4
Installed: nodejs-1:10.15.0-1.fc29.x86_64
  scriptletの実行中: nodejs-1:10.15.0-1.fc29.x86_64                         4/4
  検証             : http-parser-2.9.0-1.fc29.x86_64                        1/4
  検証             : libuv-1:1.26.0-1.fc29.x86_64                           2/4
  検証             : nodejs-1:10.15.0-1.fc29.x86_64                         3/4
  検証             : npm-1:6.4.1-1.10.15.0.1.fc29.x86_64                    4/4

インストール済み:
  nodejs-1:10.15.0-1.fc29.x86_64       npm-1:6.4.1-1.10.15.0.1.fc29.x86_64
  http-parser-2.9.0-1.fc29.x86_64      libuv-1:1.26.0-1.fc29.x86_64

完了しました!
$

nodejsとnpmをインストール。
npmはnodejsのパッケージマネージャ。

$ node -v
v10.15.0
$ npm -v
6.4.1

バージョン確認。

nを入れることで、

$ sudo npm -g install n
[sudo] bg1 のパスワード:
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
+ n@2.1.12
added 1 package from 4 contributors in 0.584s
$ sudo n stable

     install : node-v11.10.0
       mkdir : /usr/local/n/versions/node/11.10.0
       fetch : https://nodejs.org/dist/v11.10.0/node-v11.10.0-linux-x64.tar.gz
######################################################################### 100.0%
   installed : v11.10.0
.
.
.
(ターミナルを再起動.)
.
.
.
$ node -v
v11.10.0
$ sudo n lts

     install : node-v10.15.1
       mkdir : /usr/local/n/versions/node/10.15.1
       fetch : https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.gz
######################################################################### 100.0%
   installed : v10.15.1

$ sudo n 10.15.0

     install : node-v10.15.0
       mkdir : /usr/local/n/versions/node/10.15.0
       fetch : https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.gz
######################################################################### 100.0%
   installed : v10.15.0

$

バージョン切り替えでライブラリのアップデートがしやすくなる。
-gオプションなのでグローバルにインストールしてる状態。

で、今回は、

Console | Node.js v11.10.0 Documentation

Consoleクラスのグローバルオブジェクトインスタンスconsoleのメソッドlogで"ABCDE"を出力する。

console.jsを作成し、

これだけ。
本来、Consoleオブジェクトの生成が要るが、グローバルインスタンスとしてconsoleがすでに用意されているので、これだけでいい。

あとは、nodeコマンドで実行。

$ vi console.js
$ cat console.js
console.log("ABCDE");
$ node console.js
ABCDE
$

このように表示される。

Sample/nodejs/Console/console_global_instance/src/Console at master · bg1bgst333/Sample · GitHub