LoginSignup
20
16

More than 1 year has passed since last update.

Linux 環境に Node.js インストール

Last updated at Posted at 2020-08-09

いろんな Linux 環境に Node.js をインストールした記録

方針

「Ubuntuに最新のNode.jsを難なくインストールする」
https://qiita.com/seibe/items/36cef7df85fe2cefa3ea
を参考に、以下の方法を採ることにした。

  1. Linux ディストリビューションの公式パッケージで Node.js と npm を入れる
  2. npm install で最新の Node.js を入れる
  3. 古い Node.js を削除する

検証環境

  • Ubuntu14.04 (2020/07に実行)
  • Ubuntu18.04 (2020/08に実行)
  • Ubuntu20.04 (2022/01に実行)
  • 2020-02-13-raspbian-buster-lite (2020/07に実行)
  • 2020-12-02-raspios-buster-armhf-lite (2021/01に実行)
  • 2021-01-11-raspios-buster-armhf-lite (2021/03に実行)
  • Raspberry Pi OS Lite Release date: April 4th 2022 (2022/08に実行)

Linux ディストリビューションの公式パッケージで Node.js と npm を入れる


$ sudo apt update
$ sudo apt install nodejs npm
$ sudo node -v 

この時点でインストールされるバージョンは、以下のとおりだった。

  • Ubuntu18.04 だと v8.10.0
  • Ubuntu20.04 だと v10.19.0
  • RaspberryPi 2020-02-13-raspbian-buster-lite だと v10.21.0
  • RaspberryPi 2020-12-02-raspios-buster-armhf-lite だと v10.23.1
  • RaspberryPi 2021-01-11-raspios-buster-armhf-lite だと v10.24.0
  • Raspberry Pi OS Lite Release date: April 4th 2022 だと v12.22.12

npm install で最新の Node.js を入れる


$ sudo npm install n -g

Ubuntu14.04 でのエラーと対処

ここで Ubuntu14.04 でエラーが出た。


npm http GET https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n
npm http GET https://registry.npmjs.org/n
npm ERR! Error: CERT_UNTRUSTED
npm ERR!     at SecurePair.<anonymous> (tls.js:1370:32)
npm ERR!     at SecurePair.EventEmitter.emit (events.js:92:17)
npm ERR!     at SecurePair.maybeInitFinished (tls.js:982:10)
.
.
.

「npm install で SSL Error になった時の対処法。」
https://blog.yug1224.com/archives/563d9b67bf652a600632d01e/

を参考にして、SSL鍵のバリデーションを一旦OFFしたらうまくいった。


# sudo npm config set strict-ssl false
# sudo npm install n -g

その後設定を元に戻す。


# sudo npm config set strict-ssl true

古い Node.js を削除する

node をインストールした後、aptでインストールしたものを削除、ログオン。


$ sudo n stable
$ sudo apt purge -y nodejs npm
$ exec $SHELL -l

バージョン確認。


$ node -v
v12.18.2

実行してみる。

WebSocketを使ったものをテスト実行するために、モジュールをインストール


$ sudo npm install -g ws

index.js を適当に作成

var server = require('ws').Server;
var s = new server({port:5001});

s.on('connection',function(ws){

    ws.on('message',function(message){
        console.log("Received: "+message);

        s.clients.forEach(function(client){
            client.send(message+' : '+new Date());
        });
    });

    ws.on('close',function(){
        console.log('I lost a client');
    });

});


実行


$ sudo node index.js

Ubuntu14.04, Raspberry Pi OS Lite Release date: April 4th 2022 では以下のようにエラーが出た


Error: Cannot find module 'ws'
Require stack:
.
.
.

「Node.jsで、存在するはずのmoduleがrequireでエラーになることについて」
https://qiita.com/DNA1980/items/11fdb7233fc288ac3502

を読んで、

echo $NODE_PATH

で設定されていなかったので以下のように実行すればエラー解消した


export NODE_PATH=`npm root -g`
20
16
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
20
16