5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

replaceAll is not a function

Posted at

事象 : jestでテストを実行したらreplaceAllメソッドで怒られた

$ yarn jest hogedetail
yarn run v1.22.19
# ...省略...
    TypeError: this.hogeCode.replaceAll is not a function

      104 |   computed: {
      105 |     bookThumbnailUrl() {
    > 106 |       return `https...省略.../${this.hogeCode.replaceAll('-', '')}`
          | ^
      107 |     }
      108 |   },
      109 |

原因 : Node.jsのバージョンがreplaceAllに対応していないから

replaceAllはNode.jsのv15.0.0以上でないと対応していないのでした。
参考 : String.prototype.replaceAll() - JavaScript | MDN

# Node.jsのバージョンはv14でした。
$ node -v
v14.21.1

失敗した対応 : Error: error:0308010C:digital envelope routines::unsupported

じゃ、最新にしよう!と思い、https://nodejs.org/ja/ の推奨版であるv18.12.1(2022-11-29時点)にしてみたところ・・・
webpackにある「digital envelope routines」というライブラリでNode.jsのバージョンが新しすぎてエラーになりました。

$ yarn dev
yarn run v1.22.19
# ...省略...
node:internal/crypto/hash:71
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/app/node_modules/webpack/lib/util/createHash.js:135:53)
    at NormalModule._initBuildHash (/app/node_modules/webpack/lib/NormalModule.js:417:16)
    at handleParseError (/app/node_modules/webpack/lib/NormalModule.js:471:10)
    at /app/node_modules/webpack/lib/NormalModule.js:503:5
    at /app/node_modules/webpack/lib/NormalModule.js:358:12
    at /app/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/app/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at Array.<anonymous> (/app/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (/app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
    at /app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
    at /app/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:123:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.12.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

nodejs 17: digital envelope routines::unsupported · Issue #14532 · webpack/webpackをみるとv17.0.0はエラーになるようで、
「 Error: error:0308010C:digital envelope routines::unsupported 」 の対処法をみるとv16.15.1は問題ないもよう。

成功した対応 : ライブラリが使える程度にNode.jsのバージョンを上げる

以下を条件にv16.18.1を選んで、問題なく解決です。

  1. replaceAllを使うにはv15.0.0以上
  2. webpackにある「digital envelope routines」を使うにはv17.0.0未満
# Node.jsのバージョン
$ node -v
v16.18.1

# jestのテストが動いた!
$ yarn jest hogedetail
yarn run v1.22.19
# ...省略...
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   1 obsolete, 0 total
Time:        10.364 s

# 起動も問題なし!
$ yarn dev
# ...省略...
✔ Client
  Compiled successfully in 18.94s
5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?