LoginSignup
12
21

More than 5 years have passed since last update.

windowsでNode.jsをバージョン管理する

Posted at

前提

windows7 64bit

Node.js v8.11.1を使っていた。
v8.12.0がリリースされてたのでバージョンアップしたい。

Node.jsについて

そもそもNode.jsのバージョンがよくわかってなかったので調べた。
まとめると、

  • 現在の体制でのLTSリリースが始まったのはv4から
  • 偶数番号が安定版。奇数番号は最新機能版
  • 12ヶ月ごとにLTSをリリース
  • Node.js v4はサポート切れ
  • 2018年10月からNode.js 10が LTSになる

せっかくなのでv10への切替を視野にバージョン管理ツールを入れる

バージョン管理ツール(nodist)のインストール

windowsの場合はnvm-windowsかnodist、という感じだったのでnodistにすることに

0.下準備

変に影響があっても困るので、
現在インストールされているNode.jsをアンインストールする。

  • コントロールパネルのプログラムと機能からアンインストール
  • C:\Users\ユーザー名\AppData\Roaming 以下、npmフォルダとnpm-cacheフォルダの削除

を実施。プロキシの認証情報が書かれている .npmrcは残した。

1.インストール

https://github.com/marcelklehr/nodist/releases
から最新版のインストーラー(NodistSetup-v0.8.8.exe)を取得。

デフォルトのインストール先(C:\Program Files (x86)\Nodist)にインストール。
その後OS再起動。

2.実行

$ nodist -v
0.8.8

インストールはできた。

Node.jsのバージョン切替

$ nodist dist
Could not read response for https://nodejs.org/dist/index.json
Could not read response for https://iojs.org/dist/index.json
Could not read response for https://nodejs.org/dist/index.json.
Sorry.

知 っ て た 。

プロキシの設定

調べたがconfig的なものに設定する方法はないらしく、環境変数に設定する。

$ set http_proxy=http://username:password@proxy.host:port

nodist distで選択可能なNode.jsのバージョンを表示(再び)

$ nodist dist
 ...
  10.9.0
  10.10.0
  10.11.0

成功。
プロキシの認証情報はターミナルが閉じると消えてしまうので、都度設定する必要がある。

Node.jsバージョン切替方法

$ nodist 8.12.0
8.12.0
Installing 8.12.0
 8.12.0 [===============] 22475/22475 KiB 100% 0.0s
Installation successful.

$ nodist
  (x64)
  7.2.1
> 8.12.0  (global: 8.12.0)

v8.12.0への切替が出来た。
(インストールされていないものは探してきてくれるっぽい?)

npmの切替

nodist npm [ver] でnpmのバージョン管理も出来る。
verにmatchと指定するとNode.jsのバージョンにあわせて変更してくれる。

$ nodist npm match
npm match
stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^

Error: Failed to read response from https://codeload.github.com/npm/npm/tar.gz/v6.4.1

嘘でしょ。
なぜかnpm v6.4.1は見つからないらしい。(9/21現在)

そのため、

$ nodist 8.11.4
8.11.4
Installing 8.11.4
 8.11.4 [===============] 22244/22244 KiB 100% 0.0s
Installation successful.

$ nodist npm match
npm match
...

$ npm -v
5.6.0

一旦はNode.js v8.11.4 と npm v5.6.0で動くように。

yarn

普段はnpmでなくyarnを使っているので、
npm v6.4.1が取得できないという瑣末な問題は置いておく。
yarnはNode.js v4以上であれば動作するとのことなので、動作確認。

Node.js v8系に関しては問題なかったが、Node.js v10では下記のような状態に。

$ nodist 10.11.0
10.11.0
Installing 10.11.0
 10.11.0 [===============] 24174/24174 KiB 100% 0.0s
Installation successful.

$ node -v
v10.11.0

$ yarn init
yarn init v1.6.0
(node:7736) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security
 and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or B
uffer.from() methods instead.
...

yarnのバージョンが古かったことが原因。
v1.9.4に更新後、問題なく動作することが確認できた。

nodistコマンド集(備忘録)

コマンド 説明
nodist
nodist ls
インストールされているバージョンの一覧表示
nodist [ver] 指定したバージョンに切替(addも兼ねる)
nodist dist 切替可能なバージョンの一覧表示
nodist add [ver] 指定したバージョンのインストール
nodist rm [ver] 指定したバージョンのアンインストール
nodist npm match npmのバージョンを現在のNode.jsに対応したものに変える

npmの削除は C:\Program Files (x86)\Nodist\npmv 内の対応フォルダを消すことで可能。

以上、やったことのメモ

12
21
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
12
21