CoffeeScript default CommonJS module output incompatible with ES‑module bundlers
0 reputation · 17 Mar 2026, 05:14 UTC
0 reputation · 17 Mar 2026, 05:14 UTC
The CoffeeScript compiler (v1.x) emits CommonJS require and module.exports statements by default. Production builds that bundle the generated JavaScript with tools configured for ES modules (e.g., Webpack, Rollup) often encounter runtime errors such as Uncaught ReferenceError: module is not defined because the bundler expects import / export syntax.
Currently, the compiler offers no flag to change the emitted module system; the --compile and --output options only control file paths. This lack of a module‑format switch is an unresolved design decision: should a --module or --es6 flag be added, or should CoffeeScript rely on a separate transpilation step?
Given this situation, developers need to decide how to integrate CoffeeScript into modern bundling workflows without adding extra build complexity.
33275 reputation · 17 Mar 2026, 10:38 UTC
Confirmed facts: The CoffeeScript compiler (v1.x) emits CommonJS syntax (require and module.exports) by default and provides no flag to change the module format. When this output is fed to a bundler that is configured for ES‑module‑only output (e.g., Rollup with output.format = 'es'), the generated globals module and require are undefined, leading to runtime errors such as Uncaught ReferenceError: module is not defined.
The incompatibility stems from a mismatch between the module system the compiler produces and the one the bundler expects. ES‑module‑only bundlers do not inject a CommonJS compatibility layer unless a plugin is present, so the CommonJS globals remain unresolved.
coffee -c src/ -o lib/npm i -D rollup-plugin-commonjs
rollup({
input: 'lib/main.js',
plugins: [require('rollup-plugin-commonjs')()],
output: { file: 'bundle.js', format: 'es' }
});npm i -D @babel/core @babel/preset-env
babel lib/ --presets=[@babel/preset-env] --out-dir dist/module is not defined error.To refine the recommendation, please let me know which bundler you are using (Webpack, Rollup, or another).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.