Mac で npm install すると Cannot find module './lib/async' が出るようになったので、Nodeとnpmを削除して、nodebew で node を再インストールした時のメモ

# はじめに
タイトルのように、npm install すると [Cannot find module './lib/async'] と表示されてインストールが進まなくなって、色々調べて結果 Node.jsを削除して、再度インストールした時のメモです。

自分のメモ書きなので特にまとめもありませんし、役立つ情報も無いかもしれませんが残しておきます。

# 現象
npm -g でインストールしたのに、依存ライブラリが無いと言われる。
node.js のパスがおかしい感じ。

## ログ

```
$ npm root -g
/Users/kazoo/.npm-global/lib/node_modules

$ node
Welcome to Node.js v12.12.0.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file

Press ^C to abort current expression, ^D to exit the repl
> global.module.paths
[
  '/Users/kazoo/repl/node_modules',
  '/Users/kazoo/node_modules',
  '/Users/node_modules',
  '/node_modules',
  '/Users/kazoo/.npm-global/lib/node_modules',
  '/Users/kazoo/.node_modules',
  '/Users/kazoo/.node_libraries',
  '/usr/local/Cellar/node/12.12.0/lib/node'
]
```

## 仮説的な 

nodeは homebrewでインストールしているが、もともとのnodeは別でインストールしている（多分。記憶に無い）それが色々悪さしていると思われる。

こんな感じ

```
$ ls -la
total 16
drwxr-xr-x   5 kazoo  staff  160 11 13 04:49 .
drwxr-xr-x   4 kazoo  staff  128 11 13 04:45 ..
drwxr-xr-x  10 kazoo  staff  320 11 13 04:49 .git
-rw-r--r--   1 kazoo  staff  140 11 13 04:48 .gitignore
-rw-r--r--   1 kazoo  staff  468 11 13 04:48 package.json
$ npm install
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module './lib/async'
npm ERR! Require stack:
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/node_modules/normalize-package-data/node_modules/resolve/index.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/node_modules/normalize-package-data/lib/fixer.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/node_modules/normalize-package-data/lib/normalize.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/node_modules/read-package-json/read-json.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/node_modules/read-package-tree/rpt.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/lib/install.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/lib/npm.js
npm ERR! - /Users/kazoo/.npm-global/lib/node_modules/npm/bin/npm-cli.js

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kazoo/.npm/_logs/2019-11-13T02_33_59_519Z-debug.log
```

```
$ which node
/usr/local/bin/node

$ which npm
/Users/kazoo/.npm-global/bin/npm

$ which brew
/usr/local/bin/brew

$ brew config
HOMEBREW_VERSION: 2.1.16
ORIGIN: https://github.com/Homebrew/brew
HEAD: 3aa7624284c43180a3f3a71aeaa9263092868e12
Last commit: 8 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 21e4508164a772f3ebd7964ea3d59f195750ef52
Core tap last commit: 4 days ago
HOMEBREW_PREFIX: /usr/local
CPU: quad-core 64-bit haswell
Homebrew Ruby: 2.6.3 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.3/bin/ruby
Clang: 6.0 build 600
Git: 1.9.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 7.54.0 => /usr/bin/curl
Java: 1.8.0_101
macOS: 10.13.6-x86_64
CLT: 10.1.0.0.1.1539992718
Xcode: 6.1.1
```

```
$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and which additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew-provided
script of the same name. We found the following "config" scripts:
  /Users/kazoo/anaconda/bin/icu-config
  /Users/kazoo/anaconda/bin/freetype-config
  /Users/kazoo/anaconda/bin/xslt-config
  /Users/kazoo/anaconda/bin/libpng16-config
  /Users/kazoo/anaconda/bin/python3.6m-config
  /Users/kazoo/anaconda/bin/libpng-config
  /Users/kazoo/anaconda/bin/xml2-config
  /Users/kazoo/anaconda/bin/python3-config
  /Users/kazoo/anaconda/bin/curl-config
  /Users/kazoo/anaconda/bin/python3.6-config

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/node/android-ifaddrs.h
  /usr/local/include/node/ares.h
  /usr/local/include/node/ares_build.h
  /usr/local/include/node/ares_rules.h
  /usr/local/include/node/ares_version.h
  /usr/local/include/node/nameser.h
  /usr/local/include/node/openssl/archs/BSD-x86/opensslconf.h
  /usr/local/include/node/openssl/archs/BSD-x86_64/opensslconf.h
  /usr/local/include/node/openssl/archs/VC-WIN32/opensslconf.h
  /usr/local/include/node/openssl/archs/VC-WIN64A/opensslconf.h
  /usr/local/include/node/openssl/archs/aix-gcc/opensslconf.h
  /usr/local/include/node/openssl/archs/aix64-gcc/opensslconf.h
  /usr/local/include/node/openssl/archs/darwin-i386-cc/opensslconf.h
  /usr/local/include/node/openssl/archs/darwin64-x86_64-cc/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-aarch64/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-armv4/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-elf/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-ppc/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-ppc64/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-x32/opensslconf.h
  /usr/local/include/node/openssl/archs/linux-x86_64/opensslconf.h
  /usr/local/include/node/openssl/archs/linux32-s390x/opensslconf.h
  /usr/local/include/node/openssl/archs/linux64-s390x/opensslconf.h
  /usr/local/include/node/openssl/archs/solaris-x86-gcc/opensslconf.h
  /usr/local/include/node/openssl/archs/solaris64-x86_64-gcc/opensslconf.h
  /usr/local/include/node/openssl/des_old.h
  /usr/local/include/node/openssl/dso.h
  /usr/local/include/node/openssl/krb5_asn.h
  /usr/local/include/node/openssl/kssl.h
  /usr/local/include/node/openssl/pqueue.h
  /usr/local/include/node/openssl/ssl23.h
  /usr/local/include/node/openssl/ui_compat.h
  /usr/local/include/node/pthread-barrier.h
  /usr/local/include/node/pthread-fixes.h
  /usr/local/include/node/stdint-msvc2008.h
  /usr/local/include/node/tree.h
  /usr/local/include/node/uv-aix.h
  /usr/local/include/node/uv-bsd.h
  /usr/local/include/node/uv-darwin.h
  /usr/local/include/node/uv-errno.h
  /usr/local/include/node/uv-linux.h
  /usr/local/include/node/uv-sunos.h
  /usr/local/include/node/uv-threadpool.h
  /usr/local/include/node/uv-unix.h
  /usr/local/include/node/uv-version.h
  /usr/local/include/node/uv-win.h
  /usr/local/include/node/v8-debug.h
  /usr/local/include/node/v8-experimental.h

Warning: An outdated version (1.9.3) of Git was detected in your PATH.
Git 2.14.3 or newer is required for Homebrew.
Please upgrade:
  brew install git

Warning: Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in /usr/local/sbin.
Consider setting the PATH for example like so:
  echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile

Warning: Your Xcode (6.1.1) is too outdated.
Please update to Xcode 10.1 (or delete it).
Xcode can be updated from the App Store.

Warning: Your Xcode (6.1.1) is outdated.
Please update to Xcode 10.1 (or delete it).
Xcode can be updated from the App Store.
```

```
$ echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
$ node -v
v12.12.0
$ npm -v
6.13.0
```

# 削除
pkgで入れた、Node.js と npm を削除

```
$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do  echo  ${i}; done | sed -e "s/^\.\///"

$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do  echo  ${i}; done | sed -e "s/^\.\///"

$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do  echo  ${i}; done | sed -e "s/^\.\///" | while read ii; do echo ${ii}; done
```

これで消えた

```
$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do  echo  ${i}; done | sed -e "s/^\.\///" | while read ii; do sudo rm /usr/local/${ii}
 done
```

残ったフォルダを削除

```
$ sudo rm -rf .npm
$ sudo rm -rf .npm-global
```

この辺りも削除

```
sudo rm -rf /usr/local/lib/node \
     /usr/local/lib/node_modules \
     /var/db/receipts/org.nodejs.*
```

brew list して node があったので、削除

```
brew uninstall node
```

# 再インストール

```
brew install nodebrew

nodebrew setup

Kazu-MacBook-Pro:~ kazoo$ brew install nodebrew
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
cf-tool
==> Updated Formulae
mysql ✔                   embulk                    kubectx                   qbs
ruby-build ✔              erlang                    kubernetes-helm           qscintilla2
abcm2ps                   exploitdb                 lazydocker                rancher-cli
abcmidi                   eye-d3                    lazygit                   rethinkdb
ansifilter                flow                      lego                      root
armadillo                 folly                     libheif                   scamper
assimp                    fop                       libimobiledevice          scc
aws-cdk                   fselect                   libplist                  sfst
aws-google-auth           futhark                   librealsense              sip
azure-cli                 gatsby-cli                mariadb-connector-c       sord
badtouch                  gifski                    mdbook                    sratoolkit
balena-cli                git-annex                 mkcert                    stanford-corenlp
bibutils                  git-archive-all           mkvtoolnix                starship
bitrise                   git-cola                  monero                    subversion
blast                     glooctl                   navi                      suil
broot                     goreleaser                neo4j                     sundials
bullet                    gradle                    neomutt                   swiftlint
bundletool                grpc                      netlify-cli               telegraf
byacc                     h3                        nlohmann-json             termshark
caf                       haxe                      now-cli                   terragrunt
carla                     hcloud                    octave                    tokei
cfn-lint                  highlight                 onefetch                  tunnel
cheat                     ios-webkit-debug-proxy    paket                     ungit
conan                     ipmiutil                  petsc                     urh
convox                    janet                     petsc-complex             vala
dartsim                   jenkins                   pioneer                   vfuse
dependency-check          jfrog-cli-go              plantuml                  vtk
deployer                  jhipster                  prettier                  wtf
devspace                  jo                        proj                      wtfutil
dive                      just                      pulumi                    xa
dvc                       kallisto                  pyqt                      yadm
==> Renamed Formulae
usbmuxd -> libusbmuxd
==> Deleted Formulae
pound                              raine                              wine

==> 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 5 seconds
Kazu-MacBook-Pro:~ kazoo$ brew list
autoconf	gdbm		libunistring	nodebrew	rbenv		sqlite
aws-sam-cli	gettext		makedepend	openssl		readline	tree
cmake		icu4c		mysql		pkg-config	ruby-build	wget
curl		libidn2		nkf		python		sphinx-doc	xz
```

```
$ nodebrew setup
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

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

~/.bash_profile にパス追加

```
export PATH=$HOME/.nodebrew/current/bin:$PATH
```


## nodeのインストール

```
$ nodebrew install-binary stable
Fetching: https://nodejs.org/dist/v12.13.0/node-v12.13.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully
```

```
$ nodebrew list
v12.13.0
current: none
$ nodebrew use stable
use v12.13.0
$ nodebrew list
v12.13.0
current: v12.13.0
$ node -v
v12.13.0
$ npm -v
6.12.0
$ npm root -g
/Users/kazoo/.npm-global/lib/node_modules
$ which node
/Users/kazoo/.nodebrew/current/bin/node
```

```
$ npm update -g npm
```

# 参考リンク

* [MacにpkgでインストールしたNode.jsをアンインストールする手順](https://qiita.com/yoshikoba/items/4906829faaaae8c73e56)
* [【Mac版】node.jsのアンインストールと再インストール手順メモ](https://qiita.com/akakuro43/items/600e7e4695588ab2958d)
* [【2018年版】macのhomebrewでnodebrew入れてからnode.jsを入れるまで](https://qiita.com/wagi0716/items/94193a80502f9d81a9e0)
* [【Mac】HomebrewでNode.jsとnpmをインストールする](https://fukatsu.tech/install-nodejs)
* [npmのグローバルインストールとローカルインストール](https://qiita.com/Alex_mht_code/items/422f5ce10d9c9d5729b7)
* [npmのグローバルインストールとローカルインストール](https://qiita.com/kijitoraneko/items/175ef29d45d155b3f405)
* [Node.js グローバルインストールでハマった](https://qiita.com/shintarogit/items/70f193829f4360dca771#vscode-%E3%81%A7%E3%83%91%E3%82%B9%E3%81%8C%E9%80%9A%E3%82%89%E3%81%AA%E3%81%84)
* [Loading from the global folders](https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders)
* [npm -g で グローバルじゃなくてユーザー空間を使う](https://takuya-1st.hatenablog.jp/entry/2018/11/27/013000)
* [Can not find async after installation
](https://stackoverflow.com/questions/21213155/can-not-find-async-after-installation)
* [npmとpackage.json](https://qiita.com/Sekky0905/items/452619651cdd950c2271)
* [第2回　Visual Studio Codeのデバッグ構成ファイルの基本：Node.js編 (1/3)](https://www.atmarkit.co.jp/ait/articles/1708/04/news027.html)