自由研究:Raspberry Pi Zero に nodejs 環境を最新にしたメモ

2019年3月14日

Raspberry Pi Zero に nodejs 環境をを最新にした際のメモを残しておく。(次回やるときの参考とするため。)

Raspberry Pi Zero では以下のコマンドで普通に nodejs と npm を導入できます。

pi@raspberrypi:~ $ sudo apt-get install nodejs npm

この際の nodejs と npm のバージョンは

pi@raspberrypi:~ $ node -v
v8.11.1

pi@raspberrypi:~ $ npm -v
1.4.21
(node:5168) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
※エラー付き。多分node.jsとnpmのバージョンの不一致なのかも?それ以上調べなかった。

でした。

この環境でシリアル通信モジュール(serialport )を使おうとnpmでインストールしようとしたところエラー発生。

pi@raspberrypi:~ $ npm install serialport
(node:5336) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
npm ERR! Error: Method Not Allowed
npm ERR! at errorResponse (/usr/share/npm/lib/cache/add-named.js:260:10)
npm ERR! at /usr/share/npm/lib/cache/add-named.js:203:12
npm ERR! at saved (/usr/share/npm/node_modules/npm-registry-client/lib/get.js:167:7)
npm ERR! at FSReqWrap.oncomplete (fs.js:135:15)
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR! <http://github.com/npm/npm/issues>

npm ERR! System Linux 4.14.79+
npm ERR! command “/usr/bin/node” “/usr/bin/npm” “install” “serialport”
npm ERR! cwd /home/pi/work/outfile_2019_0215_21
npm ERR! node -v v8.11.1
npm ERR! npm -v 1.4.21
npm ERR! code E405
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/pi/work/outfile_2019_0215_21/npm-debug.log
npm ERR! not ok code 0

ということで nodejs と npm のバージョンを挙げてみることにした。

作業手順は、こちら

を参考に nodejs のバージョン管理 n を使い nodejs を 8.15.0 にしてみた。

  1. nodejs のバージョン管理 n のインストール
    pi@raspberrypi:~ $ sudo npm install npm n -g
    (node:5459) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
    /usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
    npm WARN package.json path-is-inside@1.0.2 No README data
    npm WARN package.json sorted-object@2.0.1 No README data
    /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
    /usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
    npm WARN package.json config-chain@1.1.12 No license field.
    npm WARN package.json cyclist@0.2.2 No license field.
    npm WARN package.json json-schema@0.2.3 No license field.
    npm WARN package.json punycode@1.4.1 punycode is also the name of a node core module.
    npm WARN package.json qrcode-terminal@0.12.0 No license field.
    npm WARN package.json string_decoder@1.2.0 string_decoder is also the name of a node core module.
    n@2.1.12 /usr/local/lib/node_modules/n
    npm@6.8.0 /usr/local/lib/node_modules/npm
  2. nodejsのバージョン8.15.0をインストール
    pi@raspberrypi:~ $ sudo n 8.15.0
  3. node.jsのバージョンを確認
    pi@raspberrypi:~ $ n ls
    0.0.1
    0.0.2
    .
    .
    8.14.0
    8.14.1
    ο 8.15.0
    9.0.0
    9.1.0
    9.2.0
    .
    .
    11.7.0
    11.8.0
    11.9.0
    11.10.0
  4. node.jsのバージョンを確認
    nでnode.jsを8.15.0にして直ぐnode.jsのバージョンを確認すると
    pi@raspberrypi:~ $node -v
    v8.11.1
    となり替わっていないのでターミナルコンソールを開きなおしてみた。(nでインストールしたnodejsのパスが追加されていないのかも。)
    すると
    pi@raspberrypi:~ $ node -v
    v8.15.0
  5. npmのバージョン確認
    pi@raspberrypi:~ $ npm -v
    6.4.1
  6. シリアル通信モジュール(serialport)のインストール
    pi@raspberrypi:~ $ npm install serialport
    > @serialport/bindings@2.0.7 install /home/pi/work/outfile_2019_0215_21/node_modules/@serialport/bindings
    > prebuild-install –tag-prefix @serialport/bindings@ || node-gyp rebuildprebuild-install WARN install No prebuilt binaries found (target=8.15.0 runtime=node arch=arm libc= platform=linux)
    make: ディレクトリ ‘/home/pi/work/outfile_2019_0215_21/node_modules/@serialport/bindings/build’ に入ります
    CXX(target) Release/obj.target/bindings/src/serialport.o
    CXX(target) Release/obj.target/bindings/src/serialport_unix.o
    CXX(target) Release/obj.target/bindings/src/poller.o
    CXX(target) Release/obj.target/bindings/src/serialport_linux.o
    SOLINK_MODULE(target) Release/obj.target/bindings.node
    COPY Release/bindings.node
    make: ディレクトリ ‘/home/pi/work/outfile_2019_0215_21/node_modules/@serialport/bindings/build’ から出ます
    npm WARN saveError ENOENT: no such file or directory, open ‘/home/pi/work/outfile_2019_0215_21/package.json’
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN enoent ENOENT: no such file or directory, open ‘/home/pi/work/outfile_2019_0215_21/package.json’
    npm WARN outfile_2019_0215_21 No description
    npm WARN outfile_2019_0215_21 No repository field.
    npm WARN outfile_2019_0215_21 No README data
    npm WARN outfile_2019_0215_21 No license field.+ serialport@7.1.4
    added 78 packages from 45 contributors and audited 266 packages in 155.489sfound 0 vulnerabilities

無事 Raspberry Pi Zero の nodejs環境のバージョンアップが完了できました。