Amplify CLI をインストールする際に permission denied 的なエラーが発生する問題

# 結論

自分の場合、下記コマンドで解決した

```zsh
sudo npm install -g @aws-amplify/cli --unsafe-perm=true
```

# エラー内容

```zsh
% npm install -g @aws-amplify/cli

npm WARN deprecated graphql-toolkit@0.7.5: Use @graphql-toolkit/* monorepo packages instead. Check https://github.com/ardatan/graphql-toolkit for more details
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm WARN inquirer-autocomplete-prompt@1.0.2 requires a peer of inquirer@^5.0.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN eslint-plugin-compat@3.5.1 requires a peer of eslint@^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   stack: "Error: EACCES: permission denied, access '/usr/local/lib/node_modules'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/name/.npm/_logs/2020-03-04T08_41_59_447Z-debug.log
```

# エラー調査

それっぽい GitHub Issue がいくつかあり、下記を参照した

npm install globally failing (Error: EACCES: permission denied, mkdir) · Issue #2481 · aws-amplify/amplify-cli
https://github.com/aws-amplify/amplify-cli/issues/2481#issuecomment-537709801

> Please use sudo npm install -g @aws-amplify/cli --unsafe-perm=true to get around this.

## --unsafe-perm=true って何？

npm-config | npm Documentation
https://docs.npmjs.com/misc/config#unsafe-perm

> unsafe-perm§
> Default: false if running as root, true otherwise
> Type: Boolean
> Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.

パッケージスクリプトの実行時に UserID/GroupID を切り替えるかどうかのフラグっぽい。

root で実行している場合は default false
root 以外で実行している場合は default true

なので、 issue のコマンドは sudo 使って root 実行になるので、 default false になる所を明示的に true 指定して、パッケージスクリプトの実行時に UID/GID を切り替えるということ。

npm install を root で実行する場合のお作法という感じだろうか。

# issue に記載されていたコマンドを実行してみた

```zsh
% sudo npm install -g @aws-amplify/cli --unsafe-perm=true
Password:
npm WARN deprecated graphql-toolkit@0.7.5: Use @graphql-toolkit/* monorepo packages instead. Check https://github.com/ardatan/graphql-toolkit for more details
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
/usr/local/bin/amplify -> /usr/local/lib/node_modules/@aws-amplify/cli/bin/amplify

> core-js@3.6.4 postinstall /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/amplify-graphql-types-generator/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> core-js@2.6.11 postinstall /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"


> ejs@3.0.1 postinstall /usr/local/lib/node_modules/@aws-amplify/cli/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)


> @aws-amplify/cli@4.13.4 postinstall /usr/local/lib/node_modules/@aws-amplify/cli
> node scripts/post-install.js

Ignore installation errors for optional dependencies: gyp, fs-ext


----------------------------------------
Successfully installed the Amplify CLI
----------------------------------------


Javascript Getting Started - https://aws-amplify.github.io/docs/js/start


Android Getting Started - https://aws-amplify.github.io/docs/android/start


iOS Getting Started - https://aws-amplify.github.io/docs/ios/start


npm WARN inquirer-autocomplete-prompt@1.0.2 requires a peer of inquirer@^5.0.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN eslint-plugin-compat@3.5.1 requires a peer of eslint@^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 but none is installed. You must install peer dependencies yourself.

+ @aws-amplify/cli@4.13.4
added 1020 packages from 683 contributors in 40.597s
```

# 環境

```zsh
% node -v
v12.16.1
% npm -v
6.13.4
% amplify -v
4.13.4
```