Running Jest resulted in the error ‘Error [ERR_REQUIRE_ESM]: require() of ES Module’, which was resolved.

I was developing larger pages in Next.js, while creating Storybook and Components.
When Jest was executed at the end of the project, an error would occur.

% yarn test
yarn run v1.22.19
$ jest ./src
Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/string-width/index.js from /node_modules/cliui/build/index.cjs not supported.

Fix error.

When I read Stack Overflow, I found that many people said they handled it by rewriting config or package.json, but in this case, I thought it was a dependency of an external package, so I handled it smartly.

yarn cache clean

Delete the yarn cache first.

yarn cache clean

Delete relevant directories and files.

Remove node_modules and yarn.lock.

Reinstall the package

Reinstall the package.



This will resolve the error.

If you have not made the intended changes, it may be a good idea to clean up first and then investigate other causes.