Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8833

Macでnpmコマンドを使用できるようにする

$
0
0

Macでnpmコマンドを使えるようにしする

Motivation
- Azure IoT Hubにデバイスを追加するためにIoT Hub Explorerをインストールしたい
- npmコマンドを使ってInstallできるので、node.jsをインストールする

前提条件

  • MacOS
  • Homebrewが使用できること(僕が参考にしたページはこちら)

流れ

  1. Nodebrewのインストール
  2. Nodejsのインストール
  3. npmが動くか確認

1. nodebrewのインストール

nodebrewはNode.jsのバージョン管理に使用されるToolで、Node.jsはフロントエンドの開発によく使用されるもので、指定のバージョンで利用したい際に便利なもの。

nodebrewをインストールしてみる

$ brew install nodebrew
==> Downloading https://github.com/hokaccha/nodebrew/archive/v1.0.1.tar.gz
==> Downloading from https://codeload.github.com/hokaccha/nodebrew/tar.gz/v1.0.1
######################################################################## 100.0%==> 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

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/nodebrew/1.0.1: 8 files, 38.6KB, built in 2 seconds

コマンドの実行自体はすぐ完了する。
上で言われている通り、環境変数の設定が必要になる

$ echo'export PATH=$HOME/.nodebrew/current/bin:$PATH'>> ~/.bash_profile
$ source ~/.bash_profile

最後にバージョンの確認をしてインストールされたか確認する

$ nodebrew -v
nodebrew 1.0.1

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 backword 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 ls-remote : List remote versions

インストールするためのコマンドはnodebrew install-binary <version> : Alias of install (For backword compatibility)を使用する。

$ nodebrew install-binary latest
Fetching: https://nodejs.org/dist/v14.1.0/node-v14.1.0-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /Users/[Username]/.nodebrew/src/v14.1.0/node-v14.1.0-darwin-x64.tar.gz:
Warning:  No such file or directory

curl: (23) Failed writing body (0 != 978)
download failed: https://nodejs.org/dist/v14.1.0/node-v14.1.0-darwin-x64.tar.gz

うまくいかなかった・・・ 
~/.nodebrewのディレクトリがなかったためにエラーが出ていた。

~/.nodebrewを作成する

上記エラーを回避するために上記のディレクトリを作成する

mkdir-p ~/.nodebrew/src

再トライするとうまくいった

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

node.jsの有効化

インストールするだけではまだ使用できる状態にはなっていない。
使用するバージョンを設定するために以下のように設定する。

$ nodebrew ls
v14.1.0

current: none
$ nodebrew use v14.1.0
use v14.1.0
$ nodebrew use v14.1.0
use v14.1.0

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

最後はnpmのバージョンを確認するだけ

$ npm -v
6.14.4

Viewing all articles
Browse latest Browse all 8833

Trending Articles