TL;DR
When using esbuild to bundle code with --platform=node that depends on npm packages with a mixture of cjs and esm entry points, use the following rule of thumb:
When using --bundle , set --format to cjs . This will work in all cases except for esm modules with top-level await .
--format=esm can be used but requires a polyfill such as this one .
When using --packages=external , set --format to esm .
If you're wondering about the difference between cjs and esm , take a look at Node.js: A brief history of cjs, bundlers, and esm .
Symptom
When executing esbuild bundled code with --platform=node you may have come across one of the following runtime errors:
Error: Dynamic require of "<module_name>" is not supported
Error [ERR_REQUIRE_ESM]: require() of ES Module (...) from (...) not supported.…