truffle開発時に発生したエラー  npm install truffle-hdwallet-provider

## 概要
 truffleの開発環境フレームワークを使おうとしたところ、エラーに苦しんだので、共有します。

## 動作環境
Mac OS High Sierra10.13.6
Truffle v4.1.14
nodeのバージョン管理はnodebrew
pythonのバージョン管理はpyenv

## エラーの状況

`Metamask`、`Truffle`を使ってEthereumのテストネットにデプロイする際に、Truffleでウォレットを扱うために必要なパッケージ`truffle-hdwallet-provider`をインストールする必要があります。

```
npm install truffle-hdwallet-provider

> scrypt@6.0.3 preinstall /Users/***/infura-metacoin/node_modules/scrypt
> node node-scrypt-preinstall.js


> keccak@1.4.0 install /Users/***/infura-metacoin/node_modules/keccak
> npm run rebuild || echo "Keccak bindings compilation fail. Pure JS implementation will be used."


> keccak@1.4.0 rebuild /Users/***/infura-metacoin/node_modules/keccak
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: Python executable "/Users/***/.pyenv/shims/python" is v3.6.5, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at PythonFinder.failPythonVersion (/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:492:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:474:14)
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:264:7)
gyp ERR! stack     at ChildProcess.emit (events.js:159:13)
gyp ERR! stack     at maybeClose (internal/child_process.js:943:16)
gyp ERR! stack     at Socket.stream.socket.on (internal/child_process.js:363:11)
gyp ERR! stack     at Socket.emit (events.js:159:13)
gyp ERR! stack     at Pipe._handle.close [as _onclose] (net.js:568:12)
gyp ERR! System Darwin 17.7.0
gyp ERR! command "/Users/***/.nodebrew/node/v9.3.0/bin/node" "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/***/infura-metacoin/node_modules/keccak
gyp ERR! node -v v9.3.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! keccak@1.4.0 rebuild: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the keccak@1.4.0 rebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/***/.npm/_logs/2018-10-05T07_18_44_447Z-debug.log
Keccak bindings compilation fail. Pure JS implementation will be used.

> scrypt@6.0.3 install /Users/***/infura-metacoin/node_modules/scrypt
> node-gyp rebuild

gyp ERR! configure error
gyp ERR! stack Error: Python executable "/Users/***/.pyenv/shims/python" is v3.6.5, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at PythonFinder.failPythonVersion (/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:492:19)
gyp ERR! stack     at PythonFinder.<anonymous> (/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:474:14)
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:264:7)
gyp ERR! stack     at ChildProcess.emit (events.js:159:13)
gyp ERR! stack     at maybeClose (internal/child_process.js:943:16)
gyp ERR! stack     at Socket.stream.socket.on (internal/child_process.js:363:11)
gyp ERR! stack     at Socket.emit (events.js:159:13)
gyp ERR! stack     at Pipe._handle.close [as _onclose] (net.js:568:12)
gyp ERR! System Darwin 17.7.0
gyp ERR! command "/Users/***/.nodebrew/node/v9.3.0/bin/node" "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/***/infura-metacoin/node_modules/scrypt
gyp ERR! node -v v9.3.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open '/Users/***/infura-metacoin/package.json'
npm WARN truffle-hdwallet-provider@0.0.6 requires a peer of truffle@4.x but none is installed. You must install peer dependencies yourself.
npm WARN infura-metacoin No description
npm WARN infura-metacoin No repository field.
npm WARN infura-metacoin No README data
npm WARN infura-metacoin No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/***/.npm/_logs/2018-10-05T07_18_45_335Z-debug.log
```

エラーの内容的には、pythonのバージョンが3.x系なので、パッケージをインストールできないということなので、python2.x系に変更する必要があります。

## pyenvによるpythonのバージョン変更
```
$ pyenv local 2.7.9
$ pyenv versions
  system
* 2.7.9 (set by /Users/***/infura-metacoin/.python-version)
  3.6.5
$ python --version
Python 2.7.9
```
python2.x系に切り替わったことがわかります。簡単ですね。

## エラー再び...
いざ、もう一度トライしてみると、

```
$ npm install truffle-hdwallet-provider

> scrypt@6.0.3 preinstall /Users/***/infura-metacoin/node_modules/scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install /Users/***/infura-metacoin/node_modules/scrypt
> node-gyp rebuild

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

Traceback (most recent call last):
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py", line 16, in <module>
    sys.exit(gyp.script_main())
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 545, in script_main
    return main(sys.argv[1:])
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 538, in main
    return gyp_main(args)
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 523, in gyp_main
    generator.GenerateOutput(flat_list, targets, data, params)
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 2170, in GenerateOutput
    part_of_all=qualified_target in needed_targets)
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 817, in Write
    mac_bundle_deps, extra_outputs, part_of_all)
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 1453, in WriteTarget
    lambda p: Sourceify(self.Absolutify(p)))
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py", line 809, in GetLdflags
    archs = self.GetActiveArchs(self.configname)
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py", line 420, in GetActiveArchs
    xcode_archs_default = GetXcodeArchsDefault()
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py", line 118, in GetXcodeArchsDefault
    xcode_version, _ = XcodeVersion()
  File "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py", line 1265, in XcodeVersion
    version = re.match(r'(\d\.\d\.?\d*)', version).groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:336:16)
gyp ERR! stack     at ChildProcess.emit (events.js:159:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:12)
gyp ERR! System Darwin 17.7.0
gyp ERR! command "/Users/***/.nodebrew/node/v9.3.0/bin/node" "/Users/***/.nodebrew/node/v9.3.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/***/infura-metacoin/node_modules/scrypt
gyp ERR! node -v v9.3.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open '/Users/***/infura-metacoin/package.json'
npm WARN truffle-hdwallet-provider@0.0.6 requires a peer of truffle@4.x but none is installed. You must install peer dependencies yourself.
npm WARN infura-metacoin No description
npm WARN infura-metacoin No repository field.
npm WARN infura-metacoin No README data
npm WARN infura-metacoin No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! scrypt@6.0.3 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the scrypt@6.0.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/***/.npm/_logs/2018-10-05T07_23_08_304Z-debug.log
```

エラーの数は減ったものの、また別のエラーが出てきました。
nodebrewでnodeをインストールした際には、Xcodeは必要なかったのですが、今回のパッケージのインストールには、どうもXcodeが関連しているみたいです。Xcodeのインストールが終わっている前提で、XcodeのPATHの設定を反映させます。

```
$ sudo xcode-select --switch /Applications/Xcode.app
```

## インストール成功
Try again...!

```
$ npm install truffle-hdwallet-provider

> scrypt@6.0.3 preinstall /Users/***/infura-metacoin/node_modules/scrypt
> node node-scrypt-preinstall.js


> scrypt@6.0.3 install /Users/***/infura-metacoin/node_modules/scrypt
> node-gyp rebuild

  SOLINK_MODULE(target) Release/copied_files.node
  CC(target) Release/obj.target/scrypt_wrapper/src/util/memlimit.o
  CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/keyderivation.o
  CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/pickparams.o
  CC(target) Release/obj.target/scrypt_wrapper/src/scryptwrapper/hash.o
  LIBTOOL-STATIC Release/scrypt_wrapper.a
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/crypto/crypto_scrypt.o
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/crypto/crypto_scrypt_smix.o
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/util/warnp.o
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/alg/sha256.o
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/libcperciva/util/insecure_memzero.o
  CC(target) Release/obj.target/scrypt_lib/scrypt/scrypt-1.2.0/lib/scryptenc/scryptenc_cpuperf.o
  LIBTOOL-STATIC Release/scrypt_lib.a
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_common.o
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_params_async.o
In file included from ../src/node-boilerplate/scrypt_params_async.cc:4:
In file included from ../src/node-boilerplate/inc/scrypt_params_async.h:28:
../src/node-boilerplate/inc/scrypt_async.h:53:17: warning: 'Call' is deprecated [-Wdeprecated-declarations]
      callback->Call(1, argv);
                ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
../src/node-boilerplate/scrypt_params_async.cc:32:13: warning: 'Call' is deprecated [-Wdeprecated-declarations]
  callback->Call(2, argv);
            ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
2 warnings generated.
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_params_sync.o
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_kdf_async.o
In file included from ../src/node-boilerplate/scrypt_kdf_async.cc:4:
In file included from ../src/node-boilerplate/inc/scrypt_kdf_async.h:28:
../src/node-boilerplate/inc/scrypt_async.h:53:17: warning: 'Call' is deprecated [-Wdeprecated-declarations]
      callback->Call(1, argv);
                ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
../src/node-boilerplate/scrypt_kdf_async.cc:28:15: warning: 'Call' is deprecated [-Wdeprecated-declarations]
    callback->Call(2, argv);
              ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
2 warnings generated.
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_kdf_sync.o
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_kdf-verify_sync.o
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_kdf-verify_async.o
In file included from ../src/node-boilerplate/scrypt_kdf-verify_async.cc:4:
In file included from ../src/node-boilerplate/inc/scrypt_kdf-verify_async.h:28:
../src/node-boilerplate/inc/scrypt_async.h:53:17: warning: 'Call' is deprecated [-Wdeprecated-declarations]
      callback->Call(1, argv);
                ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
../src/node-boilerplate/scrypt_kdf-verify_async.cc:30:13: warning: 'Call' is deprecated [-Wdeprecated-declarations]
  callback->Call(2, argv);
            ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
2 warnings generated.
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_hash_sync.o
  CXX(target) Release/obj.target/scrypt/src/node-boilerplate/scrypt_hash_async.o
In file included from ../src/node-boilerplate/scrypt_hash_async.cc:28:
In file included from ../src/node-boilerplate/inc/scrypt_hash_async.h:28:
../src/node-boilerplate/inc/scrypt_async.h:53:17: warning: 'Call' is deprecated [-Wdeprecated-declarations]
      callback->Call(1, argv);
                ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
../src/node-boilerplate/scrypt_hash_async.cc:52:13: warning: 'Call' is deprecated [-Wdeprecated-declarations]
  callback->Call(2, argv);
            ^
../../nan/nan.h:1617:3: note: 'Call' has been explicitly marked deprecated here
  NAN_DEPRECATED inline v8::Local<v8::Value>
  ^
../../nan/nan.h:98:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
2 warnings generated.
  CXX(target) Release/obj.target/scrypt/scrypt_node.o
  SOLINK_MODULE(target) Release/scrypt.node
npm WARN saveError ENOENT: no such file or directory, open '/Users/***/infura-metacoin/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/Users/***/infura-metacoin/package.json'
npm WARN truffle-hdwallet-provider@0.0.6 requires a peer of truffle@4.x but none is installed. You must install peer dependencies yourself.
npm WARN infura-metacoin No description
npm WARN infura-metacoin No repository field.
npm WARN infura-metacoin No README data
npm WARN infura-metacoin No license field.

+ truffle-hdwallet-provider@0.0.6
added 4 packages in 19.415s
```

とうとう、`truffle-hdwallet-provider`のインストールに成功しました！
動作環境によってエラーが出てくるのは、正直しんどいですね...