Node.jsによるアプリ開発 (1)

Node はスケーラブルなネットワークアプリケーションを構築するために設計された非同期型のイベント駆動の JavaScript 環境。

Node.jsがインストールされているかの確認

$ node -v

Node.js のインストール

https://nodejs.org/ja/

Homebrewのインストール

インストールされているかの確認

$ 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...]
## 以下略

Homebrewのサイトにある「インストール」の見出しの下のスクリプトを、macOSのターミナルにコピー&ペースト、実行するとHomebrewのインストールが開始する。

インストールは10分ほどかかる。パスワードを要求されたら、使用しているパソコンのパスワードを入力。

gitのインストール

インストールされているかの確認

% git help

次のように出力されればインストール済み

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:
## 以下略

インストールされていなければ、Homebrewを用いてgitをインストール

% brew install git

確認

% git help

以下のように出力されればインストール済

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:
## 以下略

Node.jsのバージョンアップ

現在のバージョンを確認

% node -v
  1. command not found: node と表示される。
  2. v16.3.0 と表示される。
  3. 2とは異なるバージョンが表示される。

1,3の場合、次の手順

Node.jsのバージョンを管理するマネージャーnodenvがインストールされているか確認

% nodenv help

以下のように出力されたらインストール済

Usage: nodenv <command> [<args>]

Some useful nodenv commands are:
   commands    List all available nodenv commands
   local       Set or show the local application-specific Node version
   global      Set or show the global Node version
   shell       Set or show the shell-specific Node version
   install     Install a Node version using node-build
   uninstall   Uninstall a specific Node version
   rehash      Rehash nodenv shims (run this after installing executables)
   version     Show the current Node version and its origin
   versions    List installed Node versions
   which       Display the full path to an executable
   whence      List all Node versions that contain the given executable

See `nodenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/nodenv/nodenv#readme

次のように出力された場合

zsh: command noto found: nodenv

nodenvをインストール

## 使用しているシェルの確認
% echo $SHELL
/bin/zsh  # zshを使っている例
...
/bin/bash # bashを使っている例

% git clone https://github.com/nodenv/nodenv.git ~/.nodenv
Cloning into '/Users/xxx/.nodenv'...
remote: Enumerating objects: 4021, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 4021 (delta 5), reused 4 (delta 1), pack-reused 4003
Receiving objects: 100% (4021/4021), 732.74 KiB | 1.19 MiB/s, done.
Resolving deltas: 100% (2636/2636), done.
% echo 'export PATH="$HOME/.nodenv/bin:$PATH"' >> ~/.zshrc
% echo 'eval "$(nodenv init -)"' >> ~/.zshrc
% source ~/.zshrc

## 確認
%nodenv help
Usage: nodenv <command> [<args>]

Some useful nodenv commands are:
   commands    List all available nodenv commands
   local       Set or show the local application-specific Node version
   global      Set or show the global Node version
   shell       Set or show the shell-specific Node version
   install     Install a Node version using node-build
   uninstall   Uninstall a specific Node version
   rehash      Rehash nodenv shims (run this after installing executables)
   version     Show the current Node version and its origin
   versions    List installed Node versions
   which       Display the full path to an executable
   whence      List all Node versions that contain the given executable

See `nodenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/nodenv/nodenv#readme

Node.jsをインストール

% nodenv install 16.3.0

次のように帰ってきた場合

nodenv: no such command `install'

Githubからインストールした場合node-builが自動的にインストールされないのが原因

% cd .nodenv
% mkdir plugins
% cd plugins
% git clone https://github.com/nodenv/node-build.git

これで使用できるようになったのでもう一度

% nodenv install 16.3.0
Downloading node-v16.3.0-darwin-arm64.tar.gz...
-> https://nodejs.org/dist/v16.3.0/node-v16.3.0-darwin-arm64.tar.gz
Installing node-v16.3.0-darwin-arm64...
Installed node-v16.3.0-darwin-arm64 to /Users/ユーザ/.nodenv/versions/16.3.0

% nodenv global 16.3.0
## なにも表示されなければ16.3.0のNode.jsが利用可能な状態

MongoDBのインストール

% brew tap mongodb/brew
Updating Homebrew...
## 以下略

% brew install mongodb-community
## 以下略

If that doesn't show you any updates, run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/all/.
You should download the Command Line Tools for Xcode 15.1.

インストール中に中断されるので指示通りコマンドラインツールをアップデート

# 管理者権限でコマンドラインツールを削除
sudo rm -rf /Library/Developer/CommandLineTools

# 管理者権限でコマンドラインツールをインストール
sudo xcode-select --install

再度インストール

% brew install mongodb-community
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################### 100.0%
## 以下略

% mongo --help #mongoコマンド確認用
zsh: command not found: mongo
## コマンドがないと言われる

インストールしてもmongoコマンドが使えない。

以前はMongoDBのパッケージにはmongoコマンドの実行に必要なMongoシェルも含まれていたが、現在はMongoDBサーバーとMongoシェルが別々のパッケージに分割された。そのため、brewコマンドでMongoDBをインストールした場合は、Mongoシェルは別にインストールする必要がある。

% mongo --help
MongoDB shell version v5.0.21
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
## 以下略

完了

参考

開発者であれば最初はきっと沼る、PATHと.zshrc周りを調べまくったので備忘録

http://docs.libuv.org/en/v1.x/design.html

More Reading

Post navigation