2021年、gulp-sassをインストールしようとして躓いた


この度完全新規のコーディング案件に携わることになったため、久しぶりに1年前まで使用していたpackage.jsonを引っ張りだしnpm intall を実行したところ、gulp-sassのインストールでエラーを吐いてしまい躓きました。
3時間調査したり悩んだりした結果、解決しましたので備忘録として残します。

エラー状況

Windows10環境で npm install gulp-sass --save-dev すると、下記の通りログが出力され、インストールできない。
私のPCにはNode.jsの【最新版(v15.5.0)】がインストールされておりました。

エラーのログ

cmd
PS G:\data\html> npm install gulp-sass --save-dev
npm WARN deprecated [email protected]: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.

> [email protected] install G:\data\html\node_modules\gulp-sass\node_modules\node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.14.1/win32-x64-88_binding.node
Cannot download "https://github.com/sass/node-sass/releases/download/v4.14.1/win32-x64-88_binding.node": 

HTTP error 404 Not Found

Hint: If github.com is not accessible in your location
      try setting a proxy via HTTP_PROXY, e.g.

      export HTTP_PROXY=http://example.com:1234

or configure npm proxy via

      npm config set proxy http://example.com:8080

> [email protected] postinstall G:\data\html\node_modules\gulp-sass\node_modules\node-sass 
> node scripts/build.js

Building: C:\Program Files\nodejs\node.exe G:\data\html\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [
gyp verb cli   'C:\\Program Files\\nodejs\\node.exe',
gyp verb cli   'G:\\data\\html\\node_modules\\node-gyp\\bin\\node-gyp.js',
gyp verb cli   'rebuild',
gyp verb cli   '--verbose',
gyp verb cli   '--libsass_ext=',
gyp verb cli   '--libsass_cflags=',
gyp verb cli   '--libsass_ldflags=',
gyp verb cli   '--libsass_library='
gyp verb cli ]
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (G:\data\html\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (G:\data\html\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (G:\data\html\node_modules\which\which.js:80:29)
gyp verb `which` failed     at G:\data\html\node_modules\which\which.js:89:16
gyp verb `which` failed     at G:\data\html\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at G:\data\html\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:199:21)
gyp verb `which` failed  python2 Error: not found: python2
gyp verb `which` failed     at getNotFoundError (G:\data\html\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (G:\data\html\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (G:\data\html\node_modules\which\which.js:80:29)
gyp verb `which` failed     at G:\data\html\node_modules\which\which.js:89:16
gyp verb `which` failed     at G:\data\html\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at G:\data\html\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:199:21) {
gyp verb `which` failed   code: 'ENOENT'
gyp verb `which` failed }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python C:\Documents and Settings\●●●\AppData\Local\Programs\Python\Python39\python.EXE
gyp ERR! configure error 
gyp ERR! stack Error: Command failed: C:\Documents and Settings\●●●\AppData\Local\Programs\Python\Python39\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                       ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack
gyp ERR! stack     at ChildProcess.exithandler (node:child_process:333:12)
gyp ERR! stack     at ChildProcess.emit (node:events:376:20)
gyp ERR! stack     at maybeClose (node:internal/child_process:1063:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:295:5)
gyp ERR! System Windows_NT 10.0.19042
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "G:\\data\\html\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd G:\data\html\node_modules\gulp-sass\node_modules\node-sass
gyp ERR! node -v v15.5.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm ERR! [email protected] postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall 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!     C:\Documents and Settings\●●●\AppData\Roaming\npm-cache\_logs\2021-01-20T16_23_09_981Z-debug.log 

debug.logを確認すると同じエラーが出力されておりました。
Pythonが必要そうなログが出ているのでインストールしたり、npmのキャッシュを消して再インストールし直したり…
検索して出てきたものを片っ端から試しましたが直りません。
なんとなく、「どうもnode-sassが悪いっぽい」ということだけ情報を掴みました。

解決策

Node.jsのバージョンを【推奨版】にダウングレードしました。
(Node.jsをアンインストール → 公式サイトから左側の「推奨版」をダウンロード&インストール)

これだけで無事gulp-sassをインストールすることができました。

原因

なぜダウングレードするだけで解決したのか?
原因はgulp-sass内で使用しているnode-sassにありました。

  • node-sassの最新バージョンはv5.0.0(これはNode.js v15.x に対応しています)
  • gulp-sass(v3.2.1)で使用しているnode-sassはv4.14.1。これはNode.js v14.x までしか対応していません

node-sassがどのNode.jsのバージョンに対応しているか?はGithubのReleaseを開くと確認できます。
https://github.com/sass/node-sass/releases

バージョンに注意!

今まで「最新こそ正義」と思い最新版を入れてきましたが、こんな落とし穴があるとは思いませんでした。
どのソフトなどを使用していてもそうですが、自分が使うパッケージに合わせてインストールするバージョンを選ぶか、使うパッケージが最新版に追いついていないのであれば代替のものを探すことが大事ですね。