Menu

Node.js and esbuild: beware of mixing cjs and esm

DEV Community: esbuild·Marco Gonzalez·6 months ago
#7I0HtZ6R
#dev#platform#bundle#format#issuecomment
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.