Ubuntu - 22.04 - Server - MQTT サーバ(JavaScript)


 クラウディア


1. 概要
2. 準備
3. インストール
6. 参考サイト

1. 概要

 前ページでは、「apt」コマンドにより、「mosquitto」をインストールしました。  一度、「GitHUB」より「mosquitto」を取得してビルドしてインストールできるかやってみましたが、失敗しました。  「JavaScript」に「MQTT」のブロッカーがあるようなので、それがインストールできるかやってみます。

2. node.js

 「node.js」「npm」が必要なのですが・・・。  当初。

apt install -y npm
 で、取得しようとしたのですが、バージョンが古い。
Node.js — Node.js®をダウンロードする
 から、最新「LTS」、「Linux」版「tar」をダウンロードして、展開します。  「root」ユーザ権限で。

curl https://nodejs.org/dist/v24.11.1/node-v24.11.1-linux-x64.tar.xz --output /tmp/node-v24.11.1-linux-x64.tar.xz

cd /tmp

tar -xvf node-v24.11.1-linux-x64.tar.xz

mv node-v24.11.1-linux-x64 /usr/local/node
 各ユーザで。

echo 'export PATH=/usr/local/node/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
 これで、「node」「npm」が使えるようになります。

$ node --version
v24.11.1
$ npm --version
11.6.2

3. インストール

 ドキュメント管理者権限で、ドキュメントルートへ移動して。

npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See 'npm help init' for definitive documentation on these fields
and exactly what they do.

Use 'npm install <pkg>' afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (mqtt)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
type: (commonjs)
About to write to /var/server/mqtt/package.json:

{
  "name": "mqtt",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "type": "commonjs"
}


Is this OK? (yes)
 上記で、プロンプトにはすべて Enter で答えています。  本体をインストール。

npm install aedes ws

added 51 packages, and audited 52 packages in 13s

7 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

vi package.json

{
  "name": "mqtt",
  "version": "1.0.0",
  "description": "",
  "license": "ISC",
  "author": "",
  "type": "commonjs",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "aedes": "^0.51.3",
    "ws": "^8.18.3"
  }
}
 7行目を下記へ書き換えます。

  "type": "module",

6. 参考サイト

 本ページは、下記のサイトおよび「ChatGPT」くんを参考にさせていただきました。
Node.js 公式サイトからダウンロードしたアーカイブファイル(*.tar.xz)で、UbuntuにNode.jsをインストールする手順

サウンドハウス