😎

Node.js 17以上にした際のOpenSSL互換エラー対応

2022/06/15に公開

どうやら、Node.js 16のEOLが7か月早まったみたいです
https://nodejs.org/ja/about/releases/

理由はNode.js 16の内部で利用されているOpenSSL 1.1.1のEOLに合わせた対処だそうです。
Node.js公式Blogでいくつか対処法も記載してくれています

When we put together Node.js 16 the hope was that we would be able to
include OpenSSL 3. Unfortunately, the timing of the releases did not allow
that to be possible, and we released Node.js 16 with OpenSSL 1.1.1. OpenSSL
1.1.1 is scheduled to be supported up until September 11th, 2023, which is
seven months before the planned End-of-Life date of Node.js 16 (April 2024).
https://nodejs.org/en/blog/announcements/nodejs16-eol/

まだ期間があるので対応を急ぐ必要はないのですが、直近で開始する案件があったので、
いっそのこと次のLTSになるであろう18に上げてしまおうと思い、見事OpenSSLの互換エラーに見舞われたので、その対処法をメモします

起こった問題

まず18にバージョンアップして、Storybookのスクリプトを実行した際に下記のエラーが発生しました。
予想通り、Storybookが内部依存しているWebpackがサポートしていないOpenSSLのバージョンを使っているということで怒られています。

node:internal/crypto/hash:67
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:67:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/xxxxx/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/xxxxx/node_modules/webpack/lib/NormalModule.js:417:16)
    at handleParseError (/xxxxx/node_modules/webpack/lib/NormalModule.js:471:10)
    at /xxxxx/node_modules/webpack/lib/NormalModule.js:503:5
    at /xxxxx/node_modules/webpack/lib/NormalModule.js:358:12
    at /xxxxx/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/xxxxx/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at Array.<anonymous> (/xxxxx/node_modules/loader-runner/lib/LoaderRunner.js:205:4) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.3.0

対処法

下記の環境変数を設定することで対処しました

export NODE_OPTIONS=--openssl-legacy-provider

https://nodejs.org/en/blog/release/v17.0.0/

自分はdirenvを入れて.envrcファイルに追記したのでプロジェクトを開いたら自動で有効にするようにしています
この対処法はNode.jsの17以上のバージョンで有効な対処法です

所感

Node.js 18にバージョンアップした意味があまり無いですが、
どのみち近いうちに18にはバージョンアップに対応しなければいけなかったので、よしとします
ただこれは暫定的な対処法であって解決策ではないので、Storybookが対応され次第、今回設定した環境変数は削除するか検討しようと思います

https://github.com/storybookjs/storybook/issues/16555

2022/09/01 追記
StorybookはWebpack5対応Builderでは解消しているようなので、他にOpenSSL互換エラーが出るライブラリがなければ環境変数は削除しても良いかもしれません
自分は18がLTSになるまでは当分このまま様子見しようかなと思います

2022/10/27 追記
18.12.0 LTSがスタートしました
OpenSSL互換エラーが出るライブラリが他になかったので、環境変数を削除しました

Discussion

ログインするとコメントできます