【NestJS】string-widthでERRO_REQUIRE_ESM

こんにちは、フリーランスエンジニアの太田雅昭です。

string-widthでエラー

string-widthは、日本語を長さ2として数えてくれるパッケージです。これをNestJSで使おうとすると以下のエラーになりました。

Error [ERR_REQUIRE_ESM]: require() of ES Module /xxx/node_modules/string-width/index.js from /xxx/node_modules/wrap-ansi/index.js not supported.
Instead change the require of /xxx/node_modules/string-width/index.js in /xxx/node_modules/wrap-ansi/index.js to a dynamic import() which is available in all CommonJS modules.

メッセージを見る限りでは、いじれない部分でエラーになっているようです。GPT4に聞いてもさっぱり解決せず、ネットサーフィンするとすぐに解決策が出てきました。

以下のissueが建てられています。

[Bug?]: Error [ERR_REQUIRE_ESM]: require() of ES Module string-width/index.js · Issue #8994 · yarnpkg/yarn

Self-service I’d be willing to implement a fix Describe the bug Everything was working with string-width v3.2.3, until v5.0.1 was added to yarn.lock, after then I get: Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/s…

これによると、以下のようにすればいいとのことでした。

"dependencies": {
    "string-width": "4.2.3",
},
"resolutions": {
    "string-width": "4.2.3"
}

GPT4に聞いてみたところ、依存関係にあるパッケージに、強制的にver4.2.3を使うように指定しているそうです。これで解決しました。

余談

たまにはゆっくりお菓子作りとかしてみたいです。最近は料理もまったくしなくなりました。カモン、時間。