TL; DR: 環境変数 npm_config_target_arch=x64
をセットしてから npm install
なり yarn install
なりを実行するとよい。
この記事を書いた2021年7月14日の時点で、 grpc-tools (1.11.2) をインストールしようとすると node-pre-gyp がエラーを出してインストールに失敗する。(追記:2022年10月リリースの1.11.3で対応済み)
このエラーは node-pre-gyp が grpc-tools の arm64 向けコンパイル済みバイナリをダウンロードしようとして失敗することに起因する。 arm64 向けバイナリはまだ配布されていない1。
エラーを回避するには環境変数 npm_config_target_arch=x64
をセットしてから npm install
や yarn install
を実行する。これをセットすると node-pre-gyp が --target_arch=x64
引数を指定されたかのように振る舞い23、 x64 向けコンパイル済みバイナリをダウンロードするようになる。
x64 向けバイナリは Rosetta 2 によるエミュレーションで M1 Mac でも動く。
エラーログ
% yarn add -D grpc-tools
yarn add v1.22.10
info No lockfile found.
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 🔨 Building fresh packages...
error /Users/uasi/tmp/t/node_modules/grpc-tools: Command failed.
Exit code: 1
Command: node-pre-gyp install
Arguments:
Directory: /Users/uasi/tmp/t/node_modules/grpc-tools
Output:
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using node-pre-gyp@1.0.5
node-pre-gyp info using node@14.17.0 | darwin | arm64
node-pre-gyp info check checked for "/Users/uasi/tmp/t/node_modules/grpc-tools/bin/grpc_tools.node" (not found)
node-pre-gyp http GET https://node-precompiled-binaries.grpc.io/grpc-tools/v1.11.2/darwin-arm64.tar.gz
node-pre-gyp ERR! install response status 404 Not Found on https://node-precompiled-binaries.grpc.io/grpc-tools/v1.11.2/darwin-arm64.tar.gz
node-pre-gyp ERR! install error
node-pre-gyp ERR! stack Error: response status 404 Not Found on https://node-precompiled-binaries.grpc.io/grpc-tools/v1.11.2/darwin-arm64.tar.gz
node-pre-gyp ERR! stack at /Users/uasi/tmp/t/node_modules/@mapbox/node-pre-gyp/lib/install.js:67:15
node-pre-gyp ERR! stack at processTicksAndRejections (internal/process/task_queues.js:95:5)
node-pre-gyp ERR! System Darwin 20.5.0
node-pre-gyp ERR! command "/Users/uasi/.nodebrew/node/v14.17.0/bin/node" "/Users/uasi/tmp/t/node_modules/grpc-tools/node_modules/.bin/node-pre-gyp" "install"
node-pre-gyp ERR! cwd /Users/uasi/tmp/t/node_modules/grpc-tools
node-pre-gyp ERR! node -v v14.17.0
node-pre-gyp ERR! node-pre-gyp -v v1.0.5
node-pre-gyp ERR! not ok
response status 404 Not Found on https://node-precompiled-binaries.grpc.io/grpc-tools/v1.11.2/darwin-arm64.tar.gz
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.