Closed6

Error [ERR_REQUIRE_ESM]: require() of ES Module ~~ not supported.の調査

つるくもつるくも

@nestjs/cliのバージョンを10.2.1にアップグレードして、nest startを実行したところ以下のようなエラーが発生した。

/Users/zurukumo/my-app/node_modules/wrap-ansi/index.js:2
const stringWidth = require('string-width');
                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/zurukumo/my-app/node_modules/string-width/index.js from /Users/zurukumo/my-app/node_modules/wrap-ansi/index.js not supported.
Instead change the require of /Users/zurukumo/my-app/node_modules/string-width/index.js in /Users/zurukumo/my-app/node_modules/wrap-ansi/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/zurukumo/my-app/node_modules/wrap-ansi/index.js:2:21) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.10.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
つるくもつるくも

実際にファイルを開いて確認したら/node_modules/wrap-ansi/index.jsはCJSで、/node_modules/string-width/index.jsはESMだった。

wrap-ansi(CJS)

string-width(ESM)

つるくもつるくも

何気なくyarn installをしてみたら、以下のようなエラーが出ていた。

yarn install v1.22.18
[1/5] 🔍  Validating package.json...
[2/5] 🔍  Resolving packages...
[3/5] 🚚  Fetching packages...
warning Pattern ["strip-ansi@^6.0.1"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["string-width@^4.1.0"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width-cjs@npm:string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["strip-ansi@^6.0.0"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["string-width@^4.2.3"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width-cjs@npm:string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["string-width@^4.2.0"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width-cjs@npm:string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["wrap-ansi@^7.0.0"] is trying to unpack in the same destination "/Users/zurukumo/Library/Caches/Yarn/v6/npm-wrap-ansi-cjs-7.0.0-67e145cff510a6a6984bdf1152911d69d2eb9e43-integrity/node_modules/wrap-ansi-cjs" as pattern ["wrap-ansi-cjs@npm:wrap-ansi@^7.0.0"]. This could result in non-deterministic behavior, skipping.
[4/5] 🔗  Linking dependencies...
warning Workspaces can only be enabled in private projects.
[5/5] 🔨  Building fresh packages...
✨  Done in 6.87s.

雑に読んだ感じだと、"wrap-ansi@^7.0.0"と"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0"がという2つのパッケージが一つのパッケージとしてまとめられているようだ。容量削減のためだろうか?string-widthやstrip-asinも同様にまとめられているようだ。
他のJSファイルがwrap-ansi(ESM)を利用しようとしても、wrap-ansi-cjs(CJS)を利用するようにnode_modulesが構成されていると考えれば、今までの調査の結果と辻褄が合う。

つるくもつるくも

https://github.com/yarnpkg/yarn/issues/4812
上記のIssueによると、どうやらこれはyarnのバージョン1.xで起こるキャッシュ関連のバグのようだ。
yarnをアップグレードをするか、npmやpnpmへの乗り換えを検討したほうが良いのかもしれない…。
resolutionでパッケージのバージョンを固定することでも解決できるようだが、あまり本質的な感じはせず…。

このスクラップは4ヶ月前にクローズされました