npmを壊しちゃった?


社内で勉強会があり、node.jsを使った開発なので、npmを使ったのですが、勉強会のHandsOnの通りやっても動かない。

$ npm install
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"

GitHub Packagesを使っているので、GITHUB_TOKEN(パーソナルトークン)を下記のように書いていたのですが、これが利用されていないようです・・・

$ echo "//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}" > ~/.npmrc

むーん。わからん。環境を入れ直すか。

$ brew install npm
Warning: node 14.12.0 is already installed and up-to-date
To reinstall 14.12.0, run `brew reinstall node`

fromulaにnpmって指定したのにnodeはすでに入ってるよって言われた・・・。

ちなみに

$ which npm
/usr/local/bin/npm

homebrewで入れたんじゃなさそうだな・・・。
ええい、一旦削除しちゃえ。

$ sudo rm -rf /usr/local/bin/npm

ターミナルを開きなおして

$ npm -v
-bash: npm: command not found

で、homebrewでnodeを再インストールすればなんとかなるだろ・・・


brew reinstall node
==> Downloading https://homebrew.bintray.com/bottles/node-14.12.0.catalina.bottl
Already downloaded: /Users/$USER/Library/Caches/Homebrew/downloads/7f431d50b49d04feb0638fe780f37bce897956fa3641a392c60eb3a90b29b48e--node-14.12.0.catalina.bottle.tar.gz
==> Reinstalling node 
==> Pouring node-14.12.0.catalina.bottle.tar.gz
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall node`
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/14.12.0: 4,698 files, 62MB

postinstallでこけてる

$ brew postinstall node
==> Postinstalling node
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall node`

なぜだ、何がおかしい? brew infoコマンドで調べてみる

$ brew info node
node: stable 14.12.0 (bottled), HEAD
Platform built on V8 to build network applications
https://nodejs.org/
/usr/local/Cellar/node/14.12.0 (4,698 files, 62MB) *
  Poured from bottle on 2020-09-24 at 18:35:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/node.rb
License: MIT
==> Dependencies
Build: pkg-config ✔, [email protected] ✘
Required: icu4c ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Analytics
install: 395,225 (30 days), 1,027,480 (90 days), 4,296,784 (365 days)
install-on-request: 285,449 (30 days), 755,473 (90 days), 3,116,512 (365 days)
build-error: 0 (30 days)

python3.8が必要か、ふむふむ。

$ brew install [email protected]
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1_1.catalina.bott
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/f7b5ab7363961fa6defcb
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.4.catalina.bot
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/6ae1c8e7c783f32bd22c6
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.33.0.catalina.bott
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/78566572373e0010a5272
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.5.catalina.bottle.ta
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/2dcc8e0121c934d1e34ff
######################################################################## 100.0%
Warning: Building [email protected] from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select --install

==> Downloading https://files.pythonhosted.org/packages/2f/8e/38259f4a44944a9206
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/08/25/f204a6138dade2f675
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/75/28/521c6dc7fef23a6836
######################################################################## 100.0%
==> Downloading https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
######################################################################## 100.0%
Error: The following formula
  [email protected]
cannot be installed as binary package and must be built from source.
Install the Command Line Tools:
  xcode-select --install

ふむふむ、command line toolsが必要なのね

$ xcode-select --install
xcode-select: note: install requested for command line developer tools

と出てダイアログが・・・。(スクリーンショット撮ってません)
installボタンをクリックすると使用許諾が出るので、読んで、同意するボタンをクリックすると
「検索しています」・・・そしてしばらくすると
「アップデートサーバーが利用できないためこのソフトウェアをダウンロードできません」
といった内容のワーニングが出てインストールに失敗しやがった・・・
(ク○Appleめ!!)

仕方がないので、Apple DevelopersのサイトからCommand Line Tools for Xcode12のdmgファイルをダウンロードしてインストール。(AppleIDが必要です)
気を取り直してpython3.8を入れるぞ。

$ brew install [email protected]
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1_1.catalina.bott
Already downloaded: /Users/$USER/Library/Caches/Homebrew/downloads/21bd17c428e48bcdca0a388899e5f618c52ea2f80af79fc5bf25f28cd59ca2b2--gdbm-1.18.1_1.catalina.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.4.catalina.bot
Already downloaded: /Users/$USER/Library/Caches/Homebrew/downloads/2fd31f598c2c259fa13d0bfb5d6cb892df479dce11424dec3a7af613de062ef2--readline-8.0.4.catalina.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.33.0.catalina.bott
Already downloaded: /Users/$USER/Library/Caches/Homebrew/downloads/6802724262549ff9d8bb8483907888d3d4f59b87c720f6e595081ef217c6eb6c--sqlite-3.33.0.catalina.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.5.catalina.bottle.ta
Already downloaded: /Users/$USER/Library/Caches/Homebrew/downloads/adc845ca1c49e4b34545aba7f6a277dc85cc30fb3c74a3dda94c823db2ba417e--xz-5.2.5.catalina.bottle.tar.gz
==> Downloading https://homebrew.bintray.com/bottles/python%403.8-3.8.5.catalina
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/3f0592dfdf6bf38c25870
######################################################################## 100.0%
==> Installing dependencies for [email protected]: gdbm, readline, sqlite and xz
==> Installing [email protected] dependency: gdbm
==> Pouring gdbm-1.18.1_1.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/gdbm/1.18.1_1: 25 files, 641KB
==> Installing [email protected] dependency: readline
==> Pouring readline-8.0.4.catalina.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/readline/8.0.4: 48 files, 1.5MB
==> Installing [email protected] dependency: sqlite
==> Pouring sqlite-3.33.0.catalina.bottle.tar.gz
==> Caveats
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> /Users/$USER/.bash_profile

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/sqlite/3.33.0: 11 files, 4MB
==> Installing [email protected] dependency: xz
==> Pouring xz-5.2.5.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/xz/5.2.5: 92 files, 1.1MB
==> Installing [email protected]
==> Pouring [email protected]
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3'

To force the link and overwrite all conflicting files:
  brew link --overwrite [email protected]

To list all files that would be deleted:
  brew link --overwrite --dry-run [email protected]

Possible conflicting files are:
/usr/local/bin/2to3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/2to3
/usr/local/bin/idle3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/idle3
/usr/local/bin/pydoc3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/pydoc3
/usr/local/bin/python3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
/usr/local/bin/python3-config -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

・・・すでにインストールしていたPython3.7ともろConflict・・・orz
Python上げます・・python公式ダウンロードサイト(for MacOS)から最新のStableリリース(執筆時点でpython-3.8.6rc1)をダウンロードしてインストールしました

brew postinstall node
==> Postinstalling node
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall node`

ダメじゃんorz

一旦、postinstallをあきらめて、とりあえず、npmを動かすことを目指します。brew info nodeで現在の状況を確認。

$ brew info node
node: stable 14.12.0 (bottled), HEAD
Platform built on V8 to build network applications
https://nodejs.org/
/usr/local/Cellar/node/14.12.0 (4,698 files, 62MB) *
  Poured from bottle on 2020-09-24 at 18:35:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/node.rb
License: MIT
==> Dependencies
Build: pkg-config ✔, [email protected] ✔
Required: icu4c ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Analytics
install: 395,225 (30 days), 1,027,480 (90 days), 4,296,784 (365 days)
install-on-request: 285,449 (30 days), 755,473 (90 days), 3,116,512 (365 days)
build-error: 0 (30 days)

依存性は解決したもよう。nodeがbrewでインストールされた場所を探ってみる

$ ls /usr/local/Cellar/node/14.12.0/
AUTHORS         README.md       lib
CHANGELOG.md        bin         libexec
INSTALL_RECEIPT.json    etc         share
LICENSE         include

$ ls -l /usr/local/Cellar/node/14.12.0/libexec/bin
total 0
lrwxr-xr-x  1 $user  316702609  38  9 22 11:22 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 $user  316702609  38  9 22 11:22 npx -> ../lib/node_modules/npm/bin/npx-cli.js

あった! PATHを通して・・・

$ export PATH="/usr/local/Cellar/node/14.12.0/libexec/bin:$PATH"

npmコマンド実行!!

$ npm -v
6.14.8

治ったぁああああ

.bash_profileに先ほどのexportコマンドを追記して、新規のターミナルウィンドウを開き、適当なnode.jsが動作するディレクトリにアクセスしてnpm startしてみる・・・

$ cd /path/to/app
$ npm start

無事に動きました・・・・

じゃあ、件(くだん)のアプリケーションは?

$ cd /path/to/another/app
$ npm install
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"

だめだこりゃ orz

githubのPersonal access tokenのリストをみるとnever usedになってるから、アクセス失敗してるっぽいのよね・・・