オブジェクト指向とかデザインパターンとか開発プロセスとかツールとか

satoshi's ソフトウェア開発

js






当サイトはアフィリエイト広告を利用してます。

node.js

Macにnode.jsとnpmをインストールする方法

投稿日:


Homebrewを使ってインストールするのが簡単です。

Homebrewのインストール

まだHomebrewをインストールしてないのなら、まずHomebrewをインストールしましょう。

シェルで以下のコマンドを実行します。

% /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

インストールに成功すれば、helpしてみるとわかります。

% brew --help
Example usage:
  brew search TEXT|/REGEX/
  brew info [FORMULA|CASK...]
  brew install FORMULA|CASK...
  brew update
  brew upgrade [FORMULA|CASK...]
  brew uninstall FORMULA|CASK...
  brew list [FORMULA|CASK...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA|CASK

Contributing:
  brew create URL [--no-fetch]
  brew edit [FORMULA|CASK...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh

nodebrewのインストール

nodebrewをインストールします。

% brew install nodebrew
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
alpscore                                                           ruby@3.0
==> Updated Formulae
Updated 47 formulae.
==> Updated Casks
Updated 48 casks.

==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/manifests/1.1.0-1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nodebrew/blobs/sha256:f9e68ad3b92827534fc9faf5d7b9c4d1fe61e4e1fa11a99e03c6cc476593fe
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:f9e68ad3b92827534fc9faf5d7b9c4d1fe61e4e1fa11a99e
######################################################################## 100.0%
==> Pouring nodebrew--1.1.0.all.bottle.1.tar.gz
==> Caveats
You need to manually run setup_dirs to create directories required by nodebrew:
  /usr/local/opt/nodebrew/bin/nodebrew setup_dirs

Add path:
  export PATH=$HOME/.nodebrew/current/bin:$PATH

To use Homebrew's directories rather than ~/.nodebrew add to your profile:
  export NODEBREW_ROOT=/usr/local/var/nodebrew

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/nodebrew/1.1.0: 8 files, 39KB
==> Running `brew cleanup nodebrew`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

PATHとNODEBREW_ROOTの環境変数を設定するようにメッセージが出てますので、そのように設定します。
vi で .zshrc を開き、PATHに上記のパスを追加し、新たにNODEBREW_ROOTを追加します。

export PATH=$HOME/.nodebrew/current/bin:$HOME/bin:(略)
export NODEBREW_ROOT=/usr/local/var/nodebrew

新しい設定を読み込みます。

% source .zshrc

パスを通したのでコマンドが実行できます。

% nodebrew -v
nodebrew 1.1.0

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backward compatibility)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>

Example:
    # install
    nodebrew install v8.9.4

    # use a specific version number
    nodebrew use v8.9.4

node.jsのインストール

nodebrew のインストールができたので、次は node.js をインストールします。

latest を指定すれば、最新版をインストールできます。

 % nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /usr/local/var/nodebrew/src/v17.4.0/node-v17.4.0-darwin-x64.tar.gz:
Warning: No such file or directory
curl: (23) Failure writing output to destination

download failed: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz

エラーになってしまいました。
ファイルを作るのに失敗してるようです。

% ls /usr/local/var/
cache		homebrew	log		run

/usr/local/var に nodebrew ディレクトリがないから?

というか、そこに入れるんだったら sudo しないといけないような。

% sudo nodebrew install-binary latest
Password:
Fetching: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz
Warning: Failed to create the file
Warning: /Users/satoshis/.nodebrew/src/v17.4.0/node-v17.4.0-darwin-x64.tar.gz:
Warning: No such file or directory
curl: (23) Failure writing output to destination

download failed: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz

sudo でやってみると保存先が変わってます。

とりあえず、自分用のホームにディレクトリを作ります。

% mkdir -p ~/.nodebrew/src

もう一度、sudo でやってみます。

 % sudo nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz
############################################################################################################################## 100.0%
Installed successfully

うまくいきました。
どのバージョンがインストールされたのか確認してみます。

% nodebrew ls
No such file or directory at /usr/local/bin/nodebrew line 575.

あらら?
エラーメッセージでぐぐってみました。

Homebrewからnodebrewをインストールして、Node.jsをインストールするまで - Qiita

setupすればいいみたいです。

% nodebrew setup
Fetching nodebrew...
Installed nodebrew in /usr/local/var/nodebrew

========================================
Export a path to nodebrew:

export PATH=/usr/local/var/nodebrew/current/bin:$PATH
========================================

PATHを追加しろと言ってるので、追加しましょう。
.zshrc を編集します。

export PATH="/usr/local/var/nodebrew/current/bin:$HOME/.nodebrew/current/bin:(略)

もう一度、バージョン確認してみると。

nodebrew ls
not installed

current: none

インストールできてないですね。
もう一度、インストールしてみます。

% nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v17.4.0/node-v17.4.0-darwin-x64.tar.gz
############################################################################################################################## 100.0%
Installed successfully

そして、バージョン確認してみると。

% nodebrew ls
v17.4.0

current: none

インストールできたバージョンを有効化し、さらにバージョン確認もやります。

% nodebrew use v17.4.0
use v17.4.0
% nodebrew ls
v17.4.0

current: v17.4.0

current が設定されました。

バージョン確認してみます。

% node -v
v17.4.0

node.js が動くようになりました。

npm も確認してみます。

% npm -v
8.3.1

これでインストール完了ですね!







-node.js
-, , , ,

Copyright© satoshi's ソフトウェア開発 , 2024 All Rights Reserved Powered by STINGER.