Node8系→12系にあげたらいっぱいエラー出た



# 概要
お世話になったnode8系が2019/12に寿命を迎えます:pray:

<img width="971" alt="スクリーンショット 2019-11-19 15.19.44.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325594/6e7e48a9-fd36-80b1-dae8-1bd99ba7ab8a.png">

https://github.com/nodejs/Release#release-schedule

そのため、node (v8.7.0) → (v12.13.0)に変更しましたが、いろいろとエラーが出たためそのメモ書きです。
（使っているmoduleによって個人差が出るため完全に自分のための覚書です:writing_hand:)

# アップグレード方法
```
$ nodenv install 12.13.0

$ nodenv local 12.13.0
````

# エラー発生

数個エラーが出たのですが、 `node-sass`を例にとります！

```
＄ yarn 

:
:
Error: Missing binding /path/to/project/node_modules/node-sass/vendor/darwin-x64-64/binding.node
Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 10.x
 
Found bindings for the following environments:
  - OS X 64-bit with Node.js 8.x
 
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass` to download the binding for your current environment.
    at module.exports (/path/to/project/node_modules/node-sass/lib/binding.js:15:13)
    at Object.<anonymous> (/path/to/project/node_modules/node-sass/lib/index.js:14:35)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
```

<details><summary>そのほかにもこんなエラーも出ました</summary><div>

```
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://node-precompiled-binaries.grpc.io/grpc/v1.20.0/node-v72-linux-x64-glibc.tar.gz
node-pre-gyp WARN Pre-built binaries not found for grpc@1.20.0 and node@12.4.0 (node-v72 ABI, glibc) (falling back to source compile with node-gyp)
gyp ERR! build error
gyp ERR! stack Error: not found: make
gyp ERR! stack at getNotFoundError (/usr/lib/node_modules/npm/node_modules/which/which.js:13:12)
gyp ERR! stack at F (/usr/lib/node_modules/npm/node_modules/which/which.js:68:19)
gyp ERR! stack at E (/usr/lib/node_modules/npm/node_modules/which/which.js:80:29)
gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/which/which.js:89:16
gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/index.js:42:5
gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/mode.js:8:5
gyp ERR! stack at FSReqCallback.oncomplete (fs.js:165:21)
gyp ERR! System Linux 4.18.0-22-generic
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--library=static_library" "--module=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc/grpc_node.node" "--module_name=grpc_node" "--module_path=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc" "--napi_version=4" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v72"
gyp ERR! cwd /home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc
gyp ERR! node -v v12.4.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --library=static_library --module=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc/grpc_node.node --module_name=grpc_node --module_path=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc --napi_version=4 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v72' (1)
node-pre-gyp ERR! stack at ChildProcess. (/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:200:13)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1021:16)
node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
node-pre-gyp ERR! System Linux 4.18.0-22-generic
node-pre-gyp ERR! command "/usr/bin/node" "/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build" "--library=static_library"
node-pre-gyp ERR! cwd /home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc
node-pre-gyp ERR! node -v v12.4.0
node-pre-gyp ERR! node-pre-gyp -v v0.12.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --library=static_library --module=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc/grpc_node.node --module_name=grpc_node --module_path=/home/ahmed/Documents/alghanim-phase2-frontend/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc --napi_version=4 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v72' (1)
npm WARN @ng-bootstrap/ng-bootstrap@4.2.1 requires a peer of rxjs@^6.3.0 but none is installed. You must install peer dependencies yourself.
npm WARN @ngtools/webpack@6.0.8 requires a peer of typescript@~2.4.0 || ~2.5.0 || ~2.6.0 || ~2.7.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular-in-memory-web-api@0.6.1 requires a peer of @angular/common@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular-in-memory-web-api@0.6.1 requires a peer of @angular/core@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular-in-memory-web-api@0.6.1 requires a peer of @angular/http@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular2-csv@0.2.9 requires a peer of @angular/common@^6.0.0-rc.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular2-csv@0.2.9 requires a peer of @angular/core@^6.0.0-rc.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN The package @angular/compiler is included as both a dev and production dependency.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/webpack-dev-server/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/watchpack/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/rijs.resdir/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/karma/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.0.7 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.0.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@schematics/update/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@schematics/angular/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@angular/compiler-cli/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@angular/cli/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@angular-devkit/schematics/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/@angular-devkit/core/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! grpc@1.20.0 install: node-pre-gyp install --fallback-to-build --library=static_library
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the grpc@1.20.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
```
</div></details>

モジュールがNodeのバージョンに対応していないときこういったエラーが出るようです

# エラー対処

 - モジュールが使っているNodeに対応しているか確認
<img width="971" alt="スクリーンショット 2019-11-19 16.21.29.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/325594/adc1b4a4-c3cf-ec7e-7227-eb1fcbded6f9.png">
https://github.com/sass/node-sass

 - Nodeに対応しているモジュールのインストール
 
 ```
 $ yarn add node-sass@4.12.0
 ```
これでpackage.jsonが以下のように更新されます

```json


{
    "name": "project",
    ：
    ：
    "dependencies": {
        ：
     →　"node-sass": "4.12.0",
        ：
    }
}

```

これで無事エラーがなくなる、と思ったら私の場合上記と同じエラーが出てしまいました…


# エラー原因特定
- yarn why <module> で依存関係確認

```
 $ yarn why node-sass

[1/4] 🤔  Why do we have the module "node-sass"...?
[2/4] 🚚  Initialising dependency graph...
[3/4] 🔍  Finding dependency...
[4/4] 🚡  Calculating file sizes...

=> Found "node-sass@4.12.0"
info Has been hoisted to "node-sass"
info This module exists because it's specified in "dependencies".
info Disk size without dependencies: "6.03MB"
info Disk size with unique dependencies: "16.02MB"
info Disk size with transitive dependencies: "26.99MB"
info Number of shared dependencies: 113

=> Found "gulp-sass#node-sass@4.9.0"
info This module exists because "gulp-sass" depends on it.
info Disk size without dependencies: "6.06MB"
info Disk size with unique dependencies: "16.05MB"
info Disk size with transitive dependencies: "27.03MB"
info Number of shared dependencies: 113
✨  Done in 1.25s.

```

なんとgulp-sassと依存関係にあり、そちらは更新されていないことがわかりました。
ですのでpackage.jsonに以下を追加
（私の場合`node-sass`意外にも他2つ依存関係にあるモジュールがありました）

```json
{
    "name": "project",
    ：
    ：
    "dependencies": {
        ：
        "node-sass": "4.12.0",
        ：
    },

   → "resolutions": {
        "**/**/fsevents": "^1.2.9",
        "node-sass": "^4.12.0",
        "grpc": "^1.20.2"
    }
}


```
`package.json`に`resolutions`フィールドを書くことで、
特定のモジュールに依存しているモジュールのバージョンも指定できます

# 解決

無事解決しました

```
$ yarn
yarn install v1.17.3
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
✨  Done in ~~ s
```


Nodeの知識全然足りないので勉強しなきゃなーと考えさせられました:skull:

#参考サイト

GitHub　Issues

- Fails to build with node.js v12.0.0 release　（https://github.com/grpc/grpc-node/issues/834）
- yarn fails to install fsevents while npm succeeds　（https://github.com/yarnpkg/yarn/issues/5962）
- support for node.js 12.x （https://github.com/sass/node-sass/issues/2632）

resolutionsについて

- yarn公式　（https://yarnpkg.com/lang/ja/docs/selective-version-resolutions/）
- resolution公式 （https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md）