LoginSignup
1
1

node開発環境構築個人メモ

Posted at

久々にエンジニア仕事をやることになったので、昨年末に買った Mac の環境構築個人メモ。
Node.js のコード管理ができるようになるところまで。

Homebrew インストール

公式のインストールガイドを参照。

macOSをお使いの場合は新しい.pkgインストーラーをお試し下さい。

と書いてあるのでそうする。

https://github.com/Homebrew/brew/releases/latest にアクセスすると現時点での最新バージョンは 4.2.18 らしい。
ページの一番下にある Homebrew-4.2.18.pkg をダウンロードしてインストール。

これだけだと PATH が通らなかったので、スクリプトを実行する従来型インストールで最後に案内されるコマンドを実行。

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

インストール完了。

% brew -v
Homebrew 4.2.18

エイリアス設定

20年以上使い続けている以下のエイリアスを ~/.zprofile に追加。

alias ls='ls -F'
alias ll='ls -l'
alias la='ls -a'

. ~/.zprofile でカレントセッションに反映。

git インストール

デフォルトでインストールはされている git バージョンを確認。

% git -v
git version 2.39.3 (Apple Git-145)

もっと新しいのがあるっぽいので、せっかくだからインストールしておこう。

% brew git install
...
zsh completions and functions have been installed to:
  /opt/homebrew/share/zsh/site-functions

バージョン確認。

% rehash
% git -v
git version 2.44.0

最後に案内された zsh completions もセットアップしておこう。
/opt/homebrew/share/zsh/site-functions の中をのぞく。

yebihara@ebiharanombp ~ % ls /opt/homebrew/share/zsh/site-functions
_brew@			_git@			git-completion.bash@

git-completion.bash の中にはこう書いてある。

# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-completion.bash
#    3) Consider changing your PS1 to also show the current branch,
#       see git-prompt.sh for details.

この手順に従って .zshrc を作成して読み込むとこんなエラーが。

ERROR: this script is obsolete, please see git-completion.zsh

.git-completion.bash 自体は zsh にも対応していると書いてあるのにどうやらダメっぽい。
同じディレクトリにある _git ファイルがそれっぽそうなのでこっちを有効化してみる。

% mkdir ~/.zsh
% cd ~/.zsh
% cp /opt/homebrew/share/zsh/site-functions/* .

.zshrc に以下を記述。
(最初、.zprofile に記述したけど効かなかった)

# Git Completion
fpath=(~/.zsh $fpath)
zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
autoload -Uz compinit && compinit -u

# Git Prompt
source ~/.zsh/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
setopt PROMPT_SUBST
PS1='%F{cyan}%n:%c%f%F{red}$(__git_ps1 "(%s)")%f\$ '

参考: https://zenn.dev/kyome/articles/ff5641b5453e6b

asdf インストール

今回はひとまず node.js の実行環境を整えないといけない。
1年前まで仕事で使っていたパッケージ管理ツールは asdf だったが、軽く調べてみたところ今でもメジャーっぽいので asdf を選択。

% brew install asdf
...
To use asdf, add the following line (or equivalent) to your shell profile
e.g. ~/.profile or ~/.zshrc:
  . /opt/homebrew/opt/asdf/libexec/asdf.sh
...

表示されたメッセージに従い .zprofile. /opt/homebrew/opt/asdf/libexec/asdf.sh という行を追加。

node.js インストール

asdf に node.js プラグインをインストール。

asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git

最新のLTSは v20 系っぽいので、中でも最新の 20.12.2 をインストール。

% asdf install nodejs 20.12.2
% asdf global nodejs 20.12.2

Gitリポジトリ作成

GitHubにログインし、新規リポジトリを作成。

GitHub Docs の 新しい SSH キーを生成して ssh-agent に追加する を参照しながらローカルマシンから GitHub にアクセスできるようにする。

まずは新しい SSH キーを作成。

% ssh-keygen -t ed25519 -C "my_email_address@example.com"

~/.ssh/config を以下の内容で作成。

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

ssh-agent に登録。

% ssh-add --apple-use-keychain ~/.ssh/id_ed25519

さっき作成したリポジトリをクローン。

% git clone git@github.com:my_github_name/my_repository.git
1
1
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
1
1