LoginSignup
0
0

n をやめて Volta にする

Last updated at Posted at 2023-12-05

今まで node のバージョンを n コマンドで管理していましたが、ナウいパッケージ管理ツールを見つけたので乗り換えることにしました。

インストール

公式で書いてある通りにします。

curl https://get.volta.sh | bash

ターミナルを再起動します。.bashrc に勝手にパスを書いてくれるので、次回起動時に volta コマンドが使えるようになっています。
他にも zshfish でも自動インストールできるようになっています。便利!

セットアップ

補完

そのままだとコマンド補完が効かないので、補完の設定をします。

Volta はコマンド補完のための設定を volta コマンドから生成できるように作られています。
雑には次のコマンドを実行してターミナル再起動すれば補完が効くようになります。

volta completions bash >> ~/.bashrc

node

以下のコマンドで node18 が入ります。

volta install node@18

yarn

yarn も簡単に入ります。

volta install yarn

バージョン管理

$ volta list
⚡️ Currently active tools:

    Node: v18.19.0 (default)
    Yarn: v4.0.2 (default)
    Tool binaries available: NONE

volta の面白い点は、これらのツールのバージョンを package.json に書いておける点です。

node20 を使うプロジェクトを yarn でセットアップし、このプロジェクトでは node20 を使うよう制約します。

~/node-20-playground$ volta install node@20
success: installed and set node@20.10.0 (with npm@10.2.3) as default

~/node-20-playground$ volta run yarn init
➤ YN0065: Yarn will periodically gather anonymous telemetry: https://yarnpkg.com/advanced/telemetry
➤ YN0065: Run yarn config set --home enableTelemetry 0 to disable

➤ YN0000: · Yarn 4.0.2
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 113ms

~/node-20-playground$ volta pin node
success: pinned node@20.10.0 (with npm@10.2.3) in package.json

~/node-20-playground$ volta pin yarn
success: pinned yarn@4.0.2 in package.json

node18 をデフォルトで使うように戻してから、node20 を使うようにしておいたディレクトリでパッケージバージョン確認をすると、無事に node20 が使われるようになっています。

~$ volta install node@18
success: installed and set node@18.19.0 (with npm@10.2.3) as default

~/node-20-playground$ volta list
⚡️ Currently active tools:

    Node: v20.10.0 (current @ ~/node-20-playground/package.json)
    Yarn: v4.0.2 (current @ ~/node-20-playground/package.json)
    Tool binaries available: NONE

package.json をプッシュしておけば同じプロジェクトの他のメンバーも同じパッケージバージョンで開発できます。

おわり

n も名前かっこよかったですが volta もかっこいいですね。

0
0
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
0
0