LoginSignup
5
2

More than 1 year has passed since last update.

asdfでNode.jsの環境設定

Last updated at Posted at 2022-03-23

たまに Ubuntu の新しい環境で Node.js を入れることがあるので、メモ代わりに環境設定法を書いておく。

ネットで検索するとMacだったり、yarnを入れてなかったりするので。

asdf インストール

基本は公式サイトに従う。
非常に丁寧に書いてある。

インストール

$ apt install curl git
$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.9.0

.zshrcに設定追加

~/.zshrc
. $HOME/.asdf/asdf.sh

既存のバージョンファイルも有効にする

これで .node-version も有効になる。プロジェクトによっては .node-version を使っているものもあるので。

$HOME/.asdfrc
legacy_version_file = yes

プラグインインストール

nodejs

https://github.com/asdf-vm/asdf-nodejs
latest のところは使う Node.js のバージョンに合わせる。

$ asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
$ asdf install nodejs latest
$ asdf global nodejs latest

yarn

(2022/04/26 追記)
今は corepack を使った方が良さそう。

corepack を使う場合

$ corepack enable
$ asdf reshim nodejs

package.json に yarn のバージョンを書くだけでOK。
yarn コマンドを実行すると自動的に yarn がインストールされる。

package.json
{
  "packageManager": "yarn@3.2.0"
}

asdf で yarn をインストールする場合

https://github.com/twuni/asdf-yarn
yarn は asdf でインストールするのが楽かなと思う。
npm でインストールすると新しい Node.js を入れた時に競合して面倒なことになる(過去に何度もあったので)。

$ asdf plugin add yarn
$ asdf install yarn latest
5
2
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
5
2