【node-sass】 nodeバージョンを上げた時に発生したError: Can't find Python executable "python", you can set the PYTHON env variable.の直し方

最近触っていなかったReactアプリを久しぶりにheroku (Container Registry) 環境にデプロイしようとしたら、Dockerfileのreact-scripts buildを実行している箇所で、以下のエラーが発生するようになっていた。

remote: npm ERR! gyp verb check python checking for Python executable "python" in the PATH
remote: npm ERR! gyp verb `which` failed Error: not found: python
remote: npm ERR! gyp verb `which` failed     at getNotFoundError (/node_modules/which/which.js:13:12)
remote: npm ERR! gyp verb `which` failed     at F (/node_modules/which/which.js:68:19)
remote: npm ERR! gyp verb `which` failed     at E (/node_modules/which/which.js:80:29)
remote: npm ERR! gyp verb `which` failed     at /node_modules/which/which.js:89:16
remote: npm ERR! gyp verb `which` failed     at /node_modules/isexe/index.js:42:5
remote: npm ERR! gyp verb `which` failed     at /node_modules/isexe/mode.js:8:5
remote: npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:192:21)
remote: npm ERR! gyp verb `which` failed  python Error: not found: python
remote: npm ERR! gyp verb `which` failed     at getNotFoundError (/node_modules/which/which.js:13:12)
remote: npm ERR! gyp verb `which` failed     at F (/node_modules/which/which.js:68:19)
remote: npm ERR! gyp verb `which` failed     at E (/node_modules/which/which.js:80:29)
remote: npm ERR! gyp verb `which` failed     at /node_modules/which/which.js:89:16
remote: npm ERR! gyp verb `which` failed     at /node_modules/isexe/index.js:42:5
remote: npm ERR! gyp verb `which` failed     at /node_modules/isexe/mode.js:8:5
remote: npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:192:21) {
remote: npm ERR! gyp verb `which` failed   code: 'ENOENT'
remote: npm ERR! gyp verb `which` failed }
remote: npm ERR! gyp ERR! configure error 
remote: npm ERR! gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
remote: npm ERR! gyp ERR! stack     at PythonFinder.failNoPython (/node_modules/node-gyp/lib/configure.js:484:19)
remote: npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/node_modules/node-gyp/lib/configure.js:406:16)
remote: npm ERR! gyp ERR! stack     at F (/node_modules/which/which.js:68:16)
remote: npm ERR! gyp ERR! stack     at E (/node_modules/which/which.js:80:29)
remote: npm ERR! gyp ERR! stack     at /node_modules/which/which.js:89:16
remote: npm ERR! gyp ERR! stack     at /node_modules/isexe/index.js:42:5
remote: npm ERR! gyp ERR! stack     at /node_modules/isexe/mode.js:8:5
remote: npm ERR! gyp ERR! stack     at FSReqCallback.oncomplete (node:fs:192:21)
remote: npm ERR! gyp ERR! System Linux 4.14.177-139.254.amzn2.x86_64
remote: npm ERR! gyp ERR! command "/usr/local/bin/node" "/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
remote: npm ERR! gyp ERR! cwd /node_modules/node-sass
remote: npm ERR! gyp ERR! node -v v15.3.0
remote: npm ERR! gyp ERR! node-gyp -v v3.8.0
remote: npm ERR! gyp ERR! not ok 
remote: npm ERR! Build failed with error code: 1
remote: 
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /root/.npm/_logs/2020-12-03T17_30_50_380Z-debug.log

原因を色々調べてみるとどうやら Dockerfile に指定していた node:alpine イメージのnodeバージョン v15.3.0 に変わったことで、node-sass のバージョンとの不整合が起こるようになったことが原因だった。

FROM node:alpine AS node_builder

node-sassのgithubリポジトリを見るとたしかに node-sass 4.14.1は、nodeバージョン v14までしか対応していないと書いてある。

f:id:moritamorie:20201204030204p:plain

node-sass 5.0.0 が出たばかりということもあって、 node-sass の方のバージョンを上げるのも怖いので、node のバージョンを14まで下げた。

FROM node:14.15.1-alpine AS node_builder

このようにDockerfileを変えたら、heroku環境デプロイ時の react-scripts build 実行時にエラーが発生せず、デプロイできるようになった。

参考