Is there a way to include an ESM only package via require?

I have a node dependency that is included in version 2 with require. Now with version 3 the library can only be included with import.

Error [ERR_REQUIRE_ESM]: require() of ES Module

But my script uses require.

Is there a way to include an ESM only package via require?

Answer

You can use dynamic import for this purpose but it’s better to use the version that supports CommonJs unless you are using ESM itself

const thePackage = import('the-package');