LoginSignup
0
0

More than 1 year has passed since last update.

TypeError: Intl.Segmenter is not a constructorの対処法

Last updated at Posted at 2022-07-16

TypeError: Intl.Segmenter is not a constructor発生の経緯

文字数を数えるChrome拡張を作成中にユニットテストを導入しようと、jsのテストライブラリmochaでテストを書き始めたところで上記エラーが発生しました。

ちなみに作成したChrome拡張はこちら👇

Intl.Segmenterとは?

Intl.Segmenterはロケールに応じたテキストの分割を可能にし、文字列から意味のある項目 (文字列、単語、文) を取得できるAPIになっています。

The Intl.Segmenter object enables locale-sensitive text segmentation, enabling you to get meaningful items (graphemes, words or sentences) from a string.
MDNのWebドキュメント

詳しい挙動は@suinさんの記事に書かれていますのでそちらをご参照ください。

解決策

早速解決策です。
(僕の場合、)Nodeをバージョン16以上にアップグレードすることで解決できました。
自分はnodebrewを使用しているので下記コマンドでアップグレードしました。

$ node -v
v14.18.1

$ nodebrew install latest
Fetching: https://nodejs.org/dist/v18.4.0/node-v18.4.0-darwin-x64.tar.gz
############################################################# 100.0%
Installed successfully

$ nodebrew use latest 
use v18.4.0

$ node -v
v18.4.0

エラー原因は対応バージョン

MDNのWebドキュメントを見てわかるようにNodeではバージョン16以上でないと動作しません。
しばらくホスト環境を使っていないうちにNode(v14.18.1)がだいぶ古くなっていたようです😢
昨今のDockerでの開発ブームにより、ホスト環境のアップグレードを忘れてしまうことが多いのでみなさんバージョンには注意しましょう!

0
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
0
0